zoukankan      html  css  js  c++  java
  • 常用的日期工具类

    对于开发中的日期工具类进行总结:

    package com.yun.base.util;

    import java.math.BigDecimal;
    import java.sql.SQLException;
    import java.sql.Timestamp;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Locale;
    import java.util.Map;

    import org.springframework.util.StringUtils;

    import oracle.sql.TIMESTAMP;

    /**
    *

    * 名称:DateUtil.java <br>
    * 描述:TODO(日期工具类) <br>
    * 最近修改时间:2015-3-5 上午11:36:00 <br>
    * @since 2015-3-5<br>
    */
    public class DateUtil {

    public static Date date = null;
    public static DateFormat dateFormat = null;
    public static Calendar calendar = null;

    /**
    * 功能描述:格式化日期【2个参数】
    *
    * @param dateStr String 字符型日期
    * @param format String 格式【yyyyMMdd】
    * @return Date 日期
    */
    public static Date parseDate(String dateStr, String format) {
    try {
    dateFormat = new SimpleDateFormat(format);
    String dt = dateStr.replaceAll("-", "/");
    if ((!dt.equals("")) && (dt.length() < format.length()))
    {
    dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
    }
    date = (Date) dateFormat.parse(dt);
    } catch (Exception e) {
    }
    return date;
    }

    /**
    * 功能描述:格式化日期【1个参数,重载 parseDate 方法】
    *
    * @param dateStr String 字符型日期:YYYY-MM-DD 格式
    * @return Date
    */
    public static Date parseDate(String dateStr) {
    return parseDate(dateStr, "yyyy/MM/dd");
    }

    /**
    * 功能描述:格式化输出日期
    *
    * @param date Date 日期
    * @param format String 格式
    * @return 返回字符型日期
    */
    public static String format(Date date, String format) {
    String result = "";
    try {
    if (!StringUtils.isEmpty(date))
    {
    dateFormat = new SimpleDateFormat(format);
    result = dateFormat.format(date);
    }
    } catch (Exception e) {
    }
    return result;
    }

    /**
    * 功能描述:格式化输出日期【1个参数,返回字符日期格式为:yyyy/MM/dd】
    *
    * @param date Date 日期
    * @return 返回字符日期格式为:yyyy/MM/dd
    */
    public static String format(Date date) {
    return format(date, "yyyy/MM/dd");
    }

    /**
    * 功能描述:常用的格式化日期
    *
    * @param date Date 日期
    * @return String 日期字符串 yyyy-MM-dd格式
    */
    public static String formatDate(Date date) {
    return formatDateByFormat(date, "yyyy-MM-dd");
    }

    /**
    * 功能描述:以指定的格式来格式化日期
    *
    * @param date Date 日期
    * @param format String 格式
    * @return String 日期字符串
    */
    public static String formatDateByFormat(Date date, String format) {
    String result = "";
    if (!StringUtils.isEmpty(date)) {
    try {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    result = sdf.format(date);
    } catch (Exception ex) {
    // ex.printStackTrace();
    }
    }
    return result;
    }

    /**
    * 功能描述:返回年份
    *
    * @param date Date 日期
    * @return 返回年份 int 类型
    */
    public static int getYear(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.YEAR);
    }

    /**
    * 功能描述:返回月份
    *
    * @param date Date 日期
    * @return 返回月份 int 类型
    */
    public static int getMonth(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MONTH) + 1;
    }

    /**
    * 功能描述:返回日份
    *
    * @param date Date 日期
    * @return 返回日份 int 类型
    */
    public static int getDay(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
    * 功能描述:返回小时
    *
    * @param date 日期
    * @return 返回小时 int 类型
    */
    public static int getHour(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
    * 功能描述:返回分钟
    *
    * @param date 日期
    * @return 返回分钟 int 类型
    */
    public static int getMinute(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MINUTE);
    }

    /**
    * 返回秒钟
    *
    * @param date Date 日期
    * @return 返回秒钟 int 类型
    */
    public static int getSecond(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.SECOND);
    }

    /**
    * 功能描述:返回毫秒
    *
    * @param date 日期
    * @return 返回毫秒 long 类型
    */
    public static long getMillis(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.getTimeInMillis();
    }


    /**
    * 功能描述:返回字符型日期
    *
    * @param date 日期
    * @return 返回字符型日期 yyyy/MM/dd 格式
    */
    public static String getDate(Date date) {
    return format(date, "yyyy/MM/dd");
    }

    /**
    * 功能描述:返回字符型时间
    *
    * @param date Date 日期
    * @return 返回字符型时间 HH:mm:ss 格式
    */
    public static String getTime(Date date) {
    return format(date, "HH:mm:ss");
    }

    /**
    * 功能描述:返回字符型日期时间
    *
    * @param date Date 日期
    * @return 返回字符型日期时间 yyyy/MM/dd HH:mm:ss 格式
    */
    public static String getDateTime(Date date) {
    return format(date, "yyyy/MM/dd HH:mm:ss");
    }

    /**
    * 功能描述:取得指定月份的第一天
    *
    * @param strdate String 字符型日期
    * @return String yyyy-MM-dd 格式
    */
    public static String getMonthBegin(String strdate) {
    date = parseDate(strdate);
    return format(date, "yyyy-MM") + "-01";
    }

    /**
    * 功能描述:取得指定月份的最后一天
    *
    * @param strdate String 字符型日期
    * @return String 日期字符串 yyyy-MM-dd格式
    */
    public static String getMonthEnd(String strdate) {
    date = parseDate(getMonthBegin(strdate));
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, 1);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    return formatDate(calendar.getTime());
    }

    /**
    * 功能描述:日期相加
    *
    * @param date Date 日期
    * @param day int 天数
    * @return 返回相加后的日期
    */
    public static Date addDate(Date date, int day) {
    calendar = Calendar.getInstance();
    long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;
    calendar.setTimeInMillis(millis);
    return calendar.getTime();
    }

    /**
    * 功能描述:日期相减
    *
    * @param date Date 日期
    * @param date1 Date 日期
    * @return 返回相减后的日期 int 类型
    */
    public static int diffDate(Date date, Date date1) {
    return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
    }

    /**
    * 功能描述:日期相减
    *
    * @param date Date 日期
    * @param date1 Date 日期
    * @return 返回相减后的日期 int 类型--得到毫秒
    */
    public static int diffDateMillis(Date date, Date date1) {
    return (int) ((getMillis(date) - getMillis(date1)));
    }

    /**
    * 判断2个时间相差多少小时<br>
    * <br>
    * @param pBeginTime 开始时间<br>
    * @param pEndTime 结束时间<br>
    * @return String 计算结果<br>
    * @Exception 发生异常<br>
    */
    public static String TimeDiff(String pBeginTime, String pEndTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Long beginL= format.parse(pBeginTime).getTime();
    Long endL = format.parse(pEndTime).getTime();
    Long day= (endL - beginL)/86400000;
    Long hour= ((endL - beginL)%86400000)/3600000;
    Long min= ((endL - beginL)%86400000%3600000)/60000;
    Long hours = day*24+hour+(min/60);
    return hours.toString();
    }

    /**
    * 判断两个时间差(XX天 XX小时 XX分 XX秒)
    * @param beginT 开始时间
    * @param endT 结束时间
    * @return String
    * @throws Exception
    */
    public static String TimeDiff2(String beginT, String endT) throws Exception {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date beginD = df.parse(beginT);
    Date endD=df.parse(endT);
    Long sjc=endD.getTime()-beginD.getTime();
    Long day=sjc/(24*60*60*1000);
    Long hour=(sjc/(60*60*1000)-day*24);
    Long min=((sjc/(60*1000))-day*24*60-hour*60);
    Long s=(sjc/1000-day*24*60*60-hour*60*60-min*60);
    String str = "";
    if(day>0) {str += day+"天 ";}
    if(hour>0) {str += hour+"小时 ";}
    str += min+"分 "+s+"秒";
    return str;
    }

    /**
    * 判断两个时间差( XX分)
    * @param beginT 开始时间
    * @param endT 结束时间
    * @return String
    * @throws Exception
    */
    public static Long TimeDiff3(String beginT, String endT) throws Exception {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date beginD = df.parse(beginT);
    Date endD=df.parse(endT);
    Long sjc=endD.getTime()-beginD.getTime();
    Long min=((sjc/(60*1000)));
    return min;
    }

    /**
    * 描述:TODO 格式化日期 字符串 转换 为 日期类型 (格式化)
    * @method StringToDate
    * @param dateStr
    * @param formatStr
    * @return Date
    */
    public static Date StringToDate(String dateStr,String formatStr){
    DateFormat sdf = new SimpleDateFormat(formatStr);
    Date date=null;
    try {
    date = sdf.parse(dateStr);
    } catch (ParseException e) {
    //e.printStackTrace();
    }
    return date;
    }

    /**
    * 描述:TODO 获取当前日期是星期几
    * @param dt
    * @return 当前日期是星期几 String
    */
    public static String getWeekOfDate(Date dt) {
    String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if (w < 0)
    {
    w = 0;
    }

    return weekDays[w];
    }

    /**
    * 描述:TODO 获取当前日期【yyyy-MM-dd】
    * @return 返回当前日期 String
    */
    public static String getCurrentDateStr(){
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());//获取当前日期时间
    return sf.format(c.getTime()); //返回当前日期
    }
    /**
    * 描述:TODO 获取当前日期【重载,可自定义日期格式】
    * @param formatStr 制定日期格式
    * @return 返回当前日期 String
    */
    public static String getCurrentDateStr(String formatStr){
    SimpleDateFormat sf = new SimpleDateFormat(formatStr);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());//获取当前日期时间
    return sf.format(c.getTime()); //返回当前日期
    }

    /**
    * 描述:TODO 获取当前日期【yyyy-MM-dd】,获取当周1(星期1)的日期
    * @return 返回当前周1的日期 String
    */
    public static String getCurrentDateStrFirstDayOfWeek(){
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);//以周1为首日
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return sf.format(c.getTime()); //设置当前周1日期
    }

    /**
    * 描述:TODO 得到本月第一天的日期【yyyy-MM-dd】
    * @param date
    * @return 本月第一天日期 String
    */
    public static String getCurrentFirstDayOfMonth(Date date){
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cDay = Calendar.getInstance();
    cDay.setTime(date);
    //设置本月第一天的日期
    cDay.set(Calendar.DAY_OF_MONTH, 1);
    return sf.format(cDay.getTime());
    }

    /**
    * 描述:TODO 得到本月最后一天的日期【yyyy-MM-dd】
    * @param date
    * @return 本月最后一天日期 String
    */
    public static String getCurrentLastDayOfMonth(Date date) {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cDay = Calendar.getInstance();
    cDay.setTime(date);
    cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
    return sf.format(cDay.getTime());
    }


    /**
    * 描述:TODO 得到本季度第一天的日期【yyyy-MM-dd】
    * @param date
    * @return String
    */
    public static String getCurrentFirstDayOfQuarter(Date date) {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cDay = Calendar.getInstance();
    cDay.setTime(date);
    int curMonth = cDay.get(Calendar.MONTH);
    if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
    cDay.set(Calendar.MONTH, Calendar.JANUARY);
    }
    if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
    cDay.set(Calendar.MONTH, Calendar.APRIL);
    }
    if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
    cDay.set(Calendar.MONTH, Calendar.JULY);
    }
    if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
    cDay.set(Calendar.MONTH, Calendar.OCTOBER);
    }
    cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));

    return sf.format(cDay.getTime());//返回本季度第一天的日期
    }
    /**
    * 描述:TODO 得到本季度最后一天的日期【yyyy-MM-dd】
    * @param date
    * @return String
    */
    public static String getCurrentLastDayOfQuarter(Date date) {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cDay = Calendar.getInstance();
    cDay.setTime(date);
    int curMonth = cDay.get(Calendar.MONTH);
    if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
    cDay.set(Calendar.MONTH, Calendar.MARCH);
    }
    if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
    cDay.set(Calendar.MONTH, Calendar.JUNE);
    }
    if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
    cDay.set(Calendar.MONTH, Calendar.AUGUST);
    }
    if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
    cDay.set(Calendar.MONTH, Calendar.DECEMBER);
    }
    cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));

    return sf.format(cDay.getTime()); //返回本季度最后一天的日期
    }

    /**
    * 描述:TODO 得到当前年份年初[yyyy-MM-dd]
    * @return String
    */
    @SuppressWarnings("static-access")
    public static String getCurrentFirstYear() {
    Calendar c = Calendar.getInstance();
    int x = c.get(c.YEAR);
    return x + "-01" + "-01";
    }

    /**
    * 描述:TODO 得到当前年份年底[yyyy-MM-dd]
    * @return String
    */
    public static String getCurrentLastYear() {
    Calendar c = Calendar.getInstance();
    int x = c.get(Calendar.YEAR);
    return x + "-12" + "-31";
    }


    /**
    * 描述:TODO 获得指定日期的前一天
    * @param specifiedDay 字符串类型【yyyy-MM-dd】
    * @return 返回字符串格式为【yyyy-MM-dd】
    * @throws Exception
    */
    public static String getSpecifiedDayBefore(String specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = null;
    try {
    date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
    } catch (ParseException e) {
    //e.printStackTrace();
    }
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day - 1);

    String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayBefore;
    }

    /**
    * 描述:TODO 获得指定日期的前一天2
    * @param specifiedDay 日期类型【new Date,....】
    * @return 返回字符串格式为【yyyy-MM-dd】
    */
    public static String getSpecifiedDayBefore2(Date specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = specifiedDay;
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day - 1);

    String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayBefore;
    }

    /**
    * 描述:TODO 获得指定日期的后一天
    * @param specifiedDay 字符串类型【yyyy-MM-dd】
    * @return 返回字符串格式为【yyyy-MM-dd】
    */
    public static String getSpecifiedDayAfter(String specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = null;
    try {
    date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
    } catch (ParseException e) {
    // e.printStackTrace();
    }
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day + 1);

    String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayAfter;
    }

    /**
    * 描述:TODO 获得指定日期的后一天2
    * <p>
    * @author 宋延军
    * @param specifiedDay 日期类型【new Date,....】
    * @return 返回字符串格式为【yyyy-MM-dd】
    */
    public static String getSpecifiedDayAfter(Date specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = specifiedDay;
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day + 1);

    String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayAfter;
    }

    /**
    * 描述:TODO 判断输入年份是否为闰年
    * @param year
    * @return 是:true 否:false boolean
    */
    public boolean leapYear(int year) {
    boolean leap;
    if (year % 4 == 0) {
    if (year % 100 == 0) {
    if (year % 400 == 0) leap = true;
    else leap = false;
    }
    else leap = true;
    }
    else leap = false;
    return leap;
    }


    /**
    * 描述:TODO 格式化(格林威治时GMT)日期格式的字符串,转换为Date类型,在转换为字符串类型的日期串
    * @method formatStrGMTtoDateToString
    * @param strGMT 格林威治时GMT_字符串 "Wed Aug 28 2013 00:00:00 GMT 0800"
    * @param format 格式化日期格式 yyyy-MM-dd HH:mm:ss
    * @return String
    */
    public static String formatStrGMTtoDateToString(String strGMT, String format) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d yyyy HH:mm:ss Z", Locale.ENGLISH);
    Date date = sdf.parse(strGMT) ;
    return formatDateByFormat(date,format);
    }

    /**
    * 描述:TODO 获取当前日期【前多少天】的日期( 或 )当前日期【后多少天】的日期
    * <p>
    * @author 宋延军
    * @param formatStr 日期格式
    * @param days 天数【int整形,正数或负数。例如: +30 后30天 或 -30 前30天】
    * @return String
    */
    public static String getCurrentDateBefore_or_CurrentDateAfter(String formatStr, int days){
    SimpleDateFormat sf = new SimpleDateFormat(formatStr);//日期格式
    Calendar c = Calendar.getInstance();//获取当前日期

    c.add(Calendar.DAY_OF_MONTH, days);//获取当前日期前 或 日期后 的日期

    String date = sf.format(c.getTime());//得到字符串日期
    return date;
    }

    /**
    * 描述:TODO 计算两个日期间的时间差
    * @param interval 计算类型: D是按照天、 H是按照小时、 M是按照分钟、 S是按照秒、 T是按照毫秒
    * @param startTime 开始日期
    * @param endTime 截止日期
    * @param format 日期格式
    * @return String
    */
    public static String dateDiff(String interval, String startTime, String endTime, String format) {
    //已知条件: 1月=30天 、1天=24小时、1小时=60分、1分=60秒、1秒=1000毫秒

    Map<String ,Integer> map = new HashMap<String,Integer>();
    map.put("D",1000 * 24 * 60 * 60);
    map.put("H",1000 * 60 * 60);//
    map.put("M",1000 * 60);
    map.put("S",1000);
    map.put("T",1);

    //按照传入的格式生成一个SimpleDateFormat对象
    SimpleDateFormat sd = new SimpleDateFormat(format);

    //获得两个时间的毫秒时间差异 , 声明返回值
    double diff = 0f, resultLong = 0f;

    try {
    diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();

    if(interval.equals("D")||interval.equals("d")){
    /*返回差多少天*/ resultLong = diff / map.get("D");
    }
    else if(interval.equals("H")||interval.equals("h")){
    /*返回多少小时*/ resultLong = diff / map.get("H");/* resultLong = diff % map.get("D") / map.get("H"); */
    }
    else if(interval.equals("M")||interval.equals("m")){
    /*返回多少分钟*/ resultLong = diff / map.get("M");/* resultLong = diff % map.get("D") % map.get("H") / map.get("M"); */
    }
    else if(interval.equals("S")||interval.equals("s")){
    /*返回多少秒*/ resultLong = diff / map.get("S");/* resultLong = diff % map.get("D") % map.get("H") % map.get("M")/ map.get("S"); */
    }
    else{
    /*返回毫秒*/ resultLong = diff;
    }
    }
    catch (ParseException e) {

    }//try...catch...End

    //浮点格式化,保留2位小数
    BigDecimal bg = new BigDecimal(resultLong);
    double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

    //得到的double浮点型数值转换为字符串做判断,如果小数点为0,则保持整数格式。
    String resultLong2 = String.valueOf(f1);
    String resultLong3 = resultLong2.substring(resultLong2.lastIndexOf(".")+1);//截取小数点后的数字
    String resultLong4 = resultLong2.substring(0,resultLong2.indexOf("."));
    if(resultLong3.equals("0")){
    return resultLong4;
    }
    else{
    return resultLong2;
    }
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate object对象类型
    * @return 返回例如:2007年4月12日的日期格式
    * */
    public static String fmtYMD(Object strDate){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    return sdf.format(strDate);
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate 日期对象类型
    * @return 返回年-月-日的日期格式
    **/
    public static String changeFormat(Date strDate){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(strDate);

    }

    /**把对象类型的日期转换成字符格式
    * @param strDate object 对象类型
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getYMDHMS(Object strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sDateFormat.format(strDate);
    }

    public static String getYMDMS(String strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sDateFormat.format(strDate);

    }

    /**把对象类型的日期转换成字符格式
    * @param strDate 日期对象类型
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getYMDHMS(Date strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sDateFormat.format(strDate);
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate object对象类型
    * @return 返回年-月-日 时:分的日期格式
    **/
    public static String getYMDHM(Object strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return sDateFormat.format(strDate);
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate 日期对象类型
    * @return 返回年-月-日 时:分的日期格式
    **/
    public static String getYMDHM(Date strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return sDateFormat.format(strDate);
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate 日期对象类型
    * @return 返回年月日
    **/
    public static String getYMD(Date strDate){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMdd");
    return sDateFormat.format(strDate);
    }

    /**把对象类型的日期转换成字符格式
    * @param strDate object对象类型
    * @return 返回例如:2007-4-12的日期格式
    **/
    public static String changeFormat(Object strDate){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(strDate);
    }

    /** 计算与系统日期相差的天数
    * @param strDate 截至日期
    * @return 返回与系统时间相差的天数,
    * 如果大于系统时间返回相差天数
    * 如果小于系统时间返回零天
    **/
    public static int getNowDate(Date strDate){

    Date curDate = new Date();
    if(!StringUtils.isEmpty(strDate)){
    if(strDate.getTime() < curDate.getTime()){
    return 0;
    }else{
    long l = strDate.getTime() - curDate.getTime();
    long d = l/60/60/1000/24;
    return (int) d;
    }
    }else{
    return 0;
    }
    }

    /**
    * 时分秒格式
    * @param strDate 截至日期
    * @return 返回与系统时间相差的天数,
    * */
    public static String getHHmmss(Object time){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("HH:mm:ss");
    return sDateFormat.format(time);
    }

    /**把对象类型的日期转换成字符格式
    * @param date 日期对象类型
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhms(Date date){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sDateFormat.format(date);
    }

    /**把对象类型的日期转换成字符格式【提前x小时 or 延后y小时】
    * @param date 日期对象类型
    * @param hour 时【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhms(Date date, int hour){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.HOUR, hour);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x小时x分 or 延后y小时y分】
    * @param date 日期对象类型
    * @param hour 时【负整数or正整数】
    * @param minutes 分【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhms(Date date, int hour, int minutes){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.HOUR, hour);
    nowTime.add(Calendar.MINUTE, minutes);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x小时x分x秒 or 延后y小时y分y秒】
    * @param date 日期对象类型
    * @param hour 时【负整数or正整数】
    * @param minutes 分【负整数or正整数】
    * @param seconds 秒【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhms(Date date, int hour, int minutes, int seconds){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.HOUR, hour);
    nowTime.add(Calendar.MINUTE, minutes);
    nowTime.add(Calendar.SECOND, seconds);
    return sDateFormat.format(nowTime.getTime());
    }


    /**把对象类型的日期转换成字符格式【提前x年 or 延后y年】
    * @param date 日期对象类型
    * @param year 年【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsYear(Date date, int year){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.YEAR, year);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x月 or 延后y月】
    * @author 宋延军
    * @param date 日期对象类型
    * @param month 月【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsMonth(Date date, int month){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.MONTH, month);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x天 or 延后y天】
    * @param date 日期对象类型
    * @param day 天【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsDay(Date date, int day){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.DATE, day);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x小时 or 延后y小时】
    * @param date 日期对象类型
    * @param hour 时【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsHour(Date date, int hour){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.HOUR, hour);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x分钟 or 延后y分钟】
    * @param date 日期对象类型
    * @param minutes 分【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsMinutes(Date date, int minutes){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.MINUTE, minutes);
    return sDateFormat.format(nowTime.getTime());
    }

    /**把对象类型的日期转换成字符格式【提前x秒钟 or 延后y秒钟】
    * @param date 日期对象类型
    * @param seconds 秒【负整数or正整数】
    * @return 返回年-月-日 时:分:秒的日期格式
    **/
    public static String getymdhmsSeconds(Date date, int seconds){
    SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar nowTime = Calendar.getInstance();
    nowTime.add(Calendar.SECOND, seconds);
    return sDateFormat.format(nowTime.getTime());
    }

    /**
    * 所给时间加一天
    * @return Date
    * ZHP
    */
    public static String nowTimeAddOneDay(String time){
    Date date1 = DateUtil.StringToDate(time, "yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+1);
    return changeFormat(calendar.getTime());
    }

    /**
    * 将时间格式定义为 该时间的00时,00分,00秒 00:00:00
    * @param formatStr
    * @param time
    * @return
    */
    public static Date formatTimeAM(String formatStr, Date time){
    calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.set(Calendar.HOUR_OF_DAY, 00);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    return StringToDate((formatDateByFormat(calendar.getTime(),formatStr)),formatStr);
    }

    /**
    * 将时间格式定义为 该时间的23时,59分,59秒 23:59:59
    * @param formatStr
    * @param time
    * @return
    */
    public static Date formatTimePM(String formatStr, Date time){
    calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    return StringToDate((formatDateByFormat(calendar.getTime(),formatStr)),formatStr);
    }

    /**
    * 得到系统的当前时间
    * @return
    */
    public static String getSystemTime(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(new Date());
    }

    /**
    * 根据oracle的Timestamp获取字符串日期时间
    * @param t Timestamp时间
    * @param formatStr 格式化字符串,如果是null默认yyyy-MM-dd hh:mm:ss
    * @return 格式化后的字符串
    */
    public static String getDateBySqlTimestamp(Object obj, String formatStr) {
    try {
    TIMESTAMP t = (TIMESTAMP)obj;
    if (formatStr == null || formatStr.equals("")) {
    formatStr = "yyyy-MM-dd hh:mm:ss";
    }
    Timestamp tt;
    tt = t.timestampValue();
    Date date = new Date(tt.getTime());
    SimpleDateFormat sd = new SimpleDateFormat(formatStr);
    return sd.format(date);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return "";
    }

    /**
    * 根据oracle的Timestamp获取字符串日期时间
    * @param t Timestamp时间
    * @param formatStr 格式化字符串,如果是null默认yyyyMMdd
    * @return 格式化后的字符串
    */
    public static String getDateNumBySqlTimestamp(Object obj, String formatStr) {
    try {
    if(!(obj == null || obj.equals(""))){
    TIMESTAMP t = (TIMESTAMP)obj;
    if (formatStr == null || formatStr.equals("")) {
    formatStr = "yyyyMMdd";
    }
    Timestamp tt;
    tt = t.timestampValue();
    Date date = new Date(tt.getTime());
    SimpleDateFormat sd = new SimpleDateFormat(formatStr);
    return sd.format(date);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return "";
    }
    }

    文章说明:里面内容大多数是查找的资源,有好多同事参与了此代码的编写。

    作者原文:http://www.cnblogs.com/summary-2017/p/7263052.html

    写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

    如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

  • 相关阅读:
    Differential Geometry之第六章平面曲线的整体性质
    球盒问题
    Fiddler (二) Script 用法
    Mac下使用Fiddler
    更新mac自带的python
    在Linux里设置环境变量的方法(export PATH)
    关于null和undefined
    ANE在ios上的使用流程和问题解决
    starling 笔记
    Android SDK安装时碰到的问题之解决办法
  • 原文地址:https://www.cnblogs.com/summary-2017/p/7263052.html
Copyright © 2011-2022 走看看