zoukankan      html  css  js  c++  java
  • [转载]Java中常用日期功能综合

    JAVA日期(JAVA Date class)处理类,常用日期功能综合,JAVA日期工具
    2009年04月10日 星期五 下午 12:07
    package com.yao.util.date;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    public class DateUtil {

    public static String defaultSimpleFormater = "yyyy-MM-dd hh:mm:ss";

    /**
    * 默认简单日期字符串
    *
    * @return
    */
    public static String getDefaultSimpleFormater() {
    return defaultSimpleFormater;
    }

    /**
    * 设置默认简单日期格式字符串
    *
    * @param defaultFormatString
    */
    public static void setDefaultSimpleFormater(String defaultFormatString) {
    DateUtil.defaultSimpleFormater = defaultFormatString;
    }

    /**
    * 格式化日期
    *
    * @param date
    * @param formatString
    * @return
    */
    public static String format(Date date, String formatString) {
    SimpleDateFormat df = new SimpleDateFormat(formatString);
    return df.format(date);
    }

    /**
    * 格式化日期(使用默认格式)
    *
    * @param date
    * @return
    */
    public static String format(Date date) {
    return format(date, defaultSimpleFormater);
    }

    /**
    * 转换成日期
    *
    * @param dateString
    * @param formatString
    * @return
    */
    public static Date parse(String dateString, String formatString) {
    SimpleDateFormat df = new SimpleDateFormat(formatString);
    try {
    return df.parse(dateString);
    } catch (ParseException e) {
    return null;
    }
    }

    /**
    * 转换成日期(使用默认格式)
    *
    * @param dateString
    * @return
    */
    public static Date parse(String dateString) {
    return parse(dateString, defaultSimpleFormater);
    }

    /**
    * 昨天
    *
    * @return
    */
    public static Date yesterday() {
    return addDay(-1);
    }

    /**
    * 明天
    *
    * @return
    */
    public static Date tomorrow() {
    return addDay(1);
    }

    /**
    * 现在
    *
    * @return
    */
    public static Date now() {
    return new Date(System.currentTimeMillis());
    }

    /**
    * 按日加
    *
    * @param value
    * @return
    */
    public static Date addDay(int value) {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.DAY_OF_YEAR, value);
    return now.getTime();
    }

    /**
    * 按日加,指定日期
    *
    * @param date
    * @param value
    * @return
    */
    public static Date addDay(Date date, int value) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.add(Calendar.DAY_OF_YEAR, value);
    return now.getTime();
    }

    /**
    * 按月加
    *
    * @param value
    * @return
    */
    public static Date addMonth(int value) {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.MONTH, value);
    return now.getTime();
    }

    /**
    * 按月加,指定日期
    *
    * @param date
    * @param value
    * @return
    */
    public static Date addMonth(Date date, int value) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.add(Calendar.MONTH, value);
    return now.getTime();
    }

    /**
    * 按年加
    *
    * @param value
    * @return
    */
    public static Date addYear(int value) {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.YEAR, value);
    return now.getTime();
    }

    /**
    * 按年加,指定日期
    *
    * @param date
    * @param value
    * @return
    */
    public static Date addYear(Date date, int value) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.add(Calendar.YEAR, value);
    return now.getTime();
    }

    /**
    * 按小时加
    *
    * @param value
    * @return
    */
    public static Date addHour(int value) {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.HOUR_OF_DAY, value);
    return now.getTime();
    }

    /**
    * 按小时加,指定日期
    *
    * @param date
    * @param value
    * @return
    */
    public static Date addHour(Date date, int value) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.add(Calendar.HOUR_OF_DAY, value);
    return now.getTime();
    }

    /**
    * 按分钟加
    *
    * @param value
    * @return
    */
    public static Date addMinute(int value) {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.MINUTE, value);
    return now.getTime();
    }

    /**
    * 按分钟加,指定日期
    *
    * @param date
    * @param value
    * @return
    */
    public static Date addMinute(Date date, int value) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.add(Calendar.MINUTE, value);
    return now.getTime();
    }

    /**
    * 年份
    *
    * @return
    */
    public static int year() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.YEAR);
    }

    /**
    * 月份
    *
    * @return
    */
    public static int month() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.MONTH);
    }

    /**
    * 日(号)
    *
    * @return
    */
    public static int day() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
    * 小时(点)
    *
    * @return
    */
    public static int hour() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.HOUR);
    }

    /**
    * 分钟
    *
    * @return
    */
    public static int minute() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.MINUTE);
    }

    /**
    * 秒
    *
    * @return
    */
    public static int second() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.SECOND);
    }

    /**
    * 星期几(礼拜几)
    *
    * @return
    */
    public static int weekday() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
    * 是上午吗?
    *
    * @return
    */
    public static boolean isAm() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.AM_PM) == 0;
    }

    /**
    * 是下午吗?
    *
    * @return
    */
    public static boolean isPm() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.AM_PM) == 1;
    }

  • 相关阅读:
    热爱工作 发财机会大增(这里不是选择软件还是硬件的问题,是自己的性格和追求的问题)——当你的老板不如你懂行的时候,还赚的盆满钵满的时候,你就可以考虑独立了 good
    TaintDroid:智能手机监控实时隐私信息流跟踪系统(三)
    Delphi读取文件属性
    创业者该如何看待腾讯的“跟进”?
    IBM总裁郭士纳总结的四类人
    迅雷程浩:企业外包服务,下一个大的风口?(2B业务一定要懂销售和营销的人,这点和2C 不一样)
    dddd
    android:minSdkVersion 之我见
    dddd
    请人不怕啦
  • 原文地址:https://www.cnblogs.com/zyxzhsh/p/1853948.html
Copyright © 2011-2022 走看看