首页

分享if-util-3.2.8源码包的TimeUtil工具类对指定时间格式、获取当前年月日周时分秒等操作

标签:时间工具类,TimeUtil,if-util,时间日期格式化,日期转换,SimpleDateFormat,Calendar,GregorianCalendar     发布时间:2018-01-28   

一、前言

分享if-util-3.2.8.jar源码中TimeUtil时间日期工具类通过java.text.SimpleDateFormat对日期时间yyyy-MM-dd HH:mm:ss等格式转换,使用java.util.Calendar、java.util.GregorianCalendar时钟获取最新的年getLastYearDate月getMonth、日getLastDate、时分秒、周getWeekDay等。

二、源码说明

package com.bill99.seashell.common.util;@b@@b@import java.io.PrintStream;@b@import java.text.DateFormat;@b@import java.text.DateFormatSymbols;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.GregorianCalendar;@b@import java.util.Locale;@b@@b@public class TimeUtil@b@{@b@  public static String getTime()@b@  {@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    String sj_str = dateFormat.format(new Date());@b@@b@    return sj_str;@b@  }@b@@b@  public static String getDate()@b@  {@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@    String sj_str = dateFormat.format(new Date());@b@@b@    return sj_str;@b@  }@b@@b@  public static String getDate(String formatstr)@b@  {@b@    SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);@b@    String sj_str = dateFormat.format(new Date());@b@@b@    return sj_str;@b@  }@b@@b@  public static String formatTime(String timeStr)@b@  {@b@    if (null == timeStr) {@b@      return null;@b@    }@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    Date date = parseTime(timeStr);@b@    String sj_str = dateFormat.format(date);@b@@b@    return sj_str;@b@  }@b@@b@  public static String formatDate(String dateStr)@b@  {@b@    if (null == dateStr)@b@      return null;@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@    Date date = parseDate(dateStr);@b@    String sj_str = dateFormat.format(date);@b@@b@    return sj_str;@b@  }@b@@b@  public static String formatDate(String pattern, String dateStr)@b@  {@b@    if (null == dateStr)@b@      return null;@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);@b@    Date date = parseDate(dateStr);@b@    String sj_str = dateFormat.format(date);@b@@b@    return sj_str;@b@  }@b@@b@  public static Date parseDate(String dateStr)@b@  {@b@    if (null == dateStr)@b@      return null;@b@@b@    Date date = null;@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@    try@b@    {@b@      date = dateFormat.parse(dateStr);@b@    }@b@    catch (ParseException e) {@b@    }@b@    return date;@b@  }@b@@b@  public static Date parseTime(String timeStr)@b@  {@b@    if (null == timeStr) {@b@      return null;@b@    }@b@@b@    Date date = null;@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@    try@b@    {@b@      date = dateFormat.parse(timeStr);@b@    }@b@    catch (ParseException e) {@b@    }@b@    return date;@b@  }@b@@b@  public static String getTime(int skipDay)@b@  {@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(new Date());@b@@b@    cal.add(5, skipDay);@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static String getTime(String timeStr, int skipDay)@b@  {@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(parseTime(timeStr));@b@@b@    cal.add(5, skipDay);@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static String getDate(int skipDay)@b@  {@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(new Date());@b@@b@    cal.add(5, skipDay);@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static String getDate(String dateStr, int skipDay)@b@  {@b@    if (null == dateStr) {@b@      return null;@b@    }@b@@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(parseDate(dateStr));@b@@b@    cal.add(5, skipDay);@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static String getTime(int skipDay, int skipHour, int skipMinute)@b@  {@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(new Date());@b@@b@    cal.add(5, skipDay);@b@    cal.add(11, skipHour);@b@    cal.add(12, skipMinute);@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static String getTime(String timeStr, int skipDay, int skipHour, int skipMinute)@b@  {@b@    if (null == timeStr) {@b@      return null;@b@    }@b@@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(parseTime(timeStr));@b@@b@    cal.add(5, skipDay);@b@    cal.add(11, skipHour);@b@    cal.add(12, skipMinute);@b@    cal.get(8);@b@@b@    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@@b@    return dateFormat.format(cal.getTime());@b@  }@b@@b@  public static long subtraction(Date minuend, Date subtrahend)@b@  {@b@    long daterange = minuend.getTime() - subtrahend.getTime();@b@    long time = 86400000L;@b@@b@    return ((daterange % time == 0L) ? daterange / time : daterange / time + 1L);@b@  }@b@@b@  public static long getM(Date date)@b@  {@b@    GregorianCalendar cal = new GregorianCalendar();@b@    cal.setTime(date);@b@    return cal.get(7);@b@  }@b@@b@  public static String getLastDate(String temp) {@b@    if ((temp == null) || (temp.equals("")))@b@      temp = "1";@b@@b@    int i = Integer.parseInt(temp);@b@    DateFormat dateFormat = DateFormat.getDateInstance(2);@b@    Calendar grc = Calendar.getInstance();@b@    grc.add(5, -i);@b@    return dateFormat.format(grc.getTime());@b@  }@b@@b@  public static String getLastYearDate()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    int y = c.get(1);@b@    String year = String.valueOf(y - 1);@b@    return year;@b@  }@b@@b@  public static String getLastMonthDate()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    int y = c.get(1);@b@    int m = c.get(2) + 1;@b@    String month = null;@b@    String year = String.valueOf(y);@b@    if (m > 1) {@b@      if (m > 10)@b@        month = String.valueOf(m - 1);@b@      else@b@        month = "0" + String.valueOf(m - 1);@b@    }@b@    else {@b@      year = String.valueOf(y - 1);@b@      month = String.valueOf(12);@b@    }@b@@b@    return year + "-" + month;@b@  }@b@@b@  public static String getLastDayDate()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    int y = c.get(1);@b@    int m = c.get(2) + 1;@b@    int d = c.get(5);@b@    int days = 0;@b@    if (m > 1)@b@      days = getMonthsDays(m - 1, y);@b@    else@b@      days = 31;@b@@b@    String day = null;@b@    String month = null;@b@    String year = String.valueOf(y);@b@    if (d > 1) {@b@      day = String.valueOf(d - 1);@b@      if (m > 9)@b@        month = String.valueOf(m);@b@      else@b@        month = "0" + String.valueOf(m);@b@    }@b@    else if ((d < 2) && (m < 2)) {@b@      day = String.valueOf(31);@b@      month = String.valueOf(12);@b@      year = String.valueOf(y - 1);@b@    } else if ((d < 2) && (m > 2)) {@b@      day = String.valueOf(days);@b@      if (m > 10)@b@        month = String.valueOf(m - 1);@b@      else@b@        month = "0" + String.valueOf(m - 1);@b@@b@    }@b@@b@    return year + "-" + month + "-" + day;@b@  }@b@@b@  public static boolean isLeapYear(int year)@b@  {@b@    return (((year % 4 == 0) && (year % 100 != 0)) || ((year % 4 == 0) && (year % 400 == 0)));@b@  }@b@@b@  public static int getMonthsDays(int month, int year)@b@  {@b@    if ((isLeapYear(year) == true) && (month == 2))@b@      return 29;@b@    if ((!(isLeapYear(year))) && (month == 2)) {@b@      return 28;@b@    }@b@@b@    if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))@b@    {@b@      return 31;@b@    }@b@    return 30;@b@  }@b@@b@  public static String getWeekDay() {@b@    DateFormatSymbols symboles = new DateFormatSymbols(Locale.getDefault());@b@    symboles.setShortWeekdays(new String[] { "", "7", "1", "2", "3", "4", "5", "6" });@b@@b@    SimpleDateFormat date = new SimpleDateFormat("E", symboles);@b@    Date day = new Date();@b@    return date.format(new Date());@b@  }@b@@b@  public static int getYear()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    return c.get(1);@b@  }@b@@b@  public static int getMonth()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    return c.get(2);@b@  }@b@@b@  public static int getDay()@b@  {@b@    Calendar c = Calendar.getInstance();@b@    return c.get(5);@b@  }@b@@b@  public static String getLastMonthDay(int lastmonths) {@b@    int month = getMonth() + 1;@b@    if (month - lastmonths > 0) {@b@      return String.valueOf(getYear()) + "-" + String.valueOf(month - lastmonths) + "-1";@b@    }@b@@b@    return String.valueOf(getYear() - 1) + "-" + String.valueOf(12 + month - lastmonths) + "-1";@b@  }@b@@b@  public static void main(String[] args)@b@  {@b@    System.out.print(parseTime("2006-10-01"));@b@  }@b@}