[Java] java 日期时间工具类 →→→→→进入此内容的聊天室

来自 , 2019-09-30, 写在 Java, 查看 115 次.
URL http://www.code666.cn/view/303ed4c6
  1. import java.text.DateFormat;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6.  
  7. /**
  8.  *
  9.  * 日期时间工具类
  10.  *
  11.  * 功能:
  12.  *              获取当前日期 时间 ,获取年月日时分秒毫秒
  13.  *              格式化日期时间
  14.  *              日期时间加上或减少n天、n月、n年
  15.  *              计算日期相差的天数
  16.  *
  17.  * @author Administrator
  18.  * @Date Jul 19, 2008
  19.  * @Time 9:47:53 AM
  20.  * @version 1.0
  21.  */
  22. public class DateUtil {
  23.  
  24.         public static Date date = null;
  25.  
  26.         public static DateFormat dateFormat = null;
  27.  
  28.         public static Calendar calendar = null;
  29.  
  30.         /**
  31.          * 英文简写(默认)如:2010-12-01
  32.          */
  33.         public static String FORMAT_SHORT = "yyyy-MM-dd";
  34.  
  35.         /**
  36.          * 英文全称 如:2010-12-01 23:15:06
  37.          */
  38.         public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
  39.  
  40.         /**
  41.          * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
  42.          */
  43.         public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
  44.  
  45.         /**
  46.          * 中文简写 如:2010年12月01日
  47.          */
  48.         public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
  49.  
  50.         /**
  51.          * 中文全称 如:2010年12月01日 23时15分06秒
  52.          */
  53.         public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
  54.  
  55.         /**
  56.          * 精确到毫秒的完整中文时间
  57.          */
  58.         public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";
  59.  
  60.         /**
  61.          * 获得默认的 date pattern
  62.          */
  63.         public static String getDatePattern() {
  64.                 return FORMAT_LONG;
  65.         }
  66.  
  67.         /**
  68.          * 根据预设格式返回当前日期
  69.          *
  70.          * @return
  71.          */
  72.         public static String getNow() {
  73.                 return format(new Date());
  74.         }
  75.  
  76.         /**
  77.          * 根据用户格式返回当前日期
  78.          *
  79.          * @param format
  80.          * @return
  81.          */
  82.         public static String getNow(String format) {
  83.                 return format(new Date(), format);
  84.         }
  85.  
  86.         /**
  87.          * 使用预设格式格式化日期
  88.          *
  89.          * @param date
  90.          * @return
  91.          */
  92.         public static String format(Date date) {
  93.                 return format(date, getDatePattern());
  94.         }
  95.  
  96.         /**
  97.          * 使用用户格式格式化日期
  98.          *
  99.          * @param date
  100.          *            日期
  101.          * @param pattern
  102.          *            日期格式
  103.          * @return
  104.          */
  105.         public static String format(Date date, String pattern) {
  106.                 String returnValue = "";
  107.                 if (date != null) {
  108.                         SimpleDateFormat df = new SimpleDateFormat(pattern);
  109.                         returnValue = df.format(date);
  110.                 }
  111.                 return (returnValue);
  112.         }
  113.  
  114.         /**
  115.          * 使用预设格式提取字符串日期
  116.          *
  117.          * @param strDate
  118.          *            日期字符串
  119.          * @return
  120.          */
  121.         public static Date parse(String strDate) {
  122.                 return parse(strDate, getDatePattern());
  123.         }
  124.  
  125.         /**
  126.          * 使用用户格式提取字符串日期
  127.          *
  128.          * @param strDate
  129.          *            日期字符串
  130.          * @param pattern
  131.          *            日期格式
  132.          * @return
  133.          */
  134.         public static Date parse(String strDate, String pattern) {
  135.                 SimpleDateFormat df = new SimpleDateFormat(pattern);
  136.                 try {
  137.                         return df.parse(strDate);
  138.                 } catch (ParseException e) {
  139.                         e.printStackTrace();
  140.                         return null;
  141.                 }
  142.         }
  143.  
  144.         /**
  145.          * 在日期上增加数个整月
  146.          *
  147.          * @param date
  148.          *            日期
  149.          * @param n
  150.          *            要增加的月数
  151.          * @return
  152.          */
  153.         public static Date addMonth(Date date, int n) {
  154.                 Calendar cal = Calendar.getInstance();
  155.                 cal.setTime(date);
  156.                 cal.add(Calendar.MONTH, n);
  157.                 return cal.getTime();
  158.         }
  159.  
  160.         /**
  161.          * 在日期上增加天数
  162.          *
  163.          * @param date
  164.          *            日期
  165.          * @param n
  166.          *            要增加的天数
  167.          * @return
  168.          */
  169.         public static Date addDay(Date date, int n) {
  170.                 Calendar cal = Calendar.getInstance();
  171.                 cal.setTime(date);
  172.                 cal.add(Calendar.DATE, n);
  173.                 return cal.getTime();
  174.         }
  175.  
  176.         /**
  177.          * 获取距现在某一小时的时刻
  178.          *
  179.          * @param format
  180.          *            格式化时间的格式
  181.          * @param h
  182.          *            距现在的小时 例如:h=-1为上一个小时,h=1为下一个小时
  183.          * @return
  184.          */
  185.         public static String getpreHour(String format, int h) {
  186.                 SimpleDateFormat sdf = new SimpleDateFormat(format);
  187.                 Date date = new Date();
  188.                 date.setTime(date.getTime() + h * 60 * 60 * 1000);
  189.                 return sdf.format(date);
  190.         }
  191.  
  192.         /**
  193.          * 获取时间戳
  194.          */
  195.         public static String getTimeString() {
  196.                 SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
  197.                 Calendar calendar = Calendar.getInstance();
  198.                 return df.format(calendar.getTime());
  199.         }
  200.  
  201.         /**
  202.          * 获取日期年份
  203.          *
  204.          * @param date
  205.          *            日期
  206.          * @return
  207.          */
  208.         public static String getYear(Date date) {
  209.                 return format(date).substring(0, 4);
  210.         }
  211.  
  212.         /**
  213.          * 功能描述:返回月
  214.          *
  215.          * @param date
  216.          *            Date 日期
  217.          * @return 返回月份
  218.          */
  219.         public static int getMonth(Date date) {
  220.                 calendar = Calendar.getInstance();
  221.                 calendar.setTime(date);
  222.                 return calendar.get(Calendar.MONTH) + 1;
  223.         }
  224.  
  225.         /**
  226.          * 功能描述:返回日
  227.          *
  228.          * @param date
  229.          *            Date 日期
  230.          * @return 返回日份
  231.          */
  232.         public static int getDay(Date date) {
  233.                 calendar = Calendar.getInstance();
  234.                 calendar.setTime(date);
  235.                 return calendar.get(Calendar.DAY_OF_MONTH);
  236.         }
  237.  
  238.         /**
  239.          * 功能描述:返回小
  240.          *
  241.          * @param date
  242.          *            日期
  243.          * @return 返回小时
  244.          */
  245.         public static int getHour(Date date) {
  246.                 calendar = Calendar.getInstance();
  247.                 calendar.setTime(date);
  248.                 return calendar.get(Calendar.HOUR_OF_DAY);
  249.         }
  250.  
  251.         /**
  252.          * 功能描述:返回分
  253.          *
  254.          * @param date
  255.          *            日期
  256.          * @return 返回分钟
  257.          */
  258.         public static int getMinute(Date date) {
  259.                 calendar = Calendar.getInstance();
  260.                 calendar.setTime(date);
  261.                 return calendar.get(Calendar.MINUTE);
  262.         }
  263.  
  264.         /**
  265.          * 返回秒钟
  266.          *
  267.          * @param date
  268.          *            Date 日期
  269.          * @return 返回秒钟
  270.          */
  271.         public static int getSecond(Date date) {
  272.                 calendar = Calendar.getInstance();
  273.                 calendar.setTime(date);
  274.                 return calendar.get(Calendar.SECOND);
  275.         }
  276.  
  277.         /**
  278.          * 功能描述:返回毫
  279.          *
  280.          * @param date
  281.          *            日期
  282.          * @return 返回毫
  283.          */
  284.         public static long getMillis(Date date) {
  285.                 calendar = Calendar.getInstance();
  286.                 calendar.setTime(date);
  287.                 return calendar.getTimeInMillis();
  288.         }
  289.  
  290.         /**
  291.          * 按默认格式的字符串距离今天的天数
  292.          *
  293.          * @param date
  294.          *            日期字符串
  295.          * @return
  296.          */
  297.         public static int countDays(String date) {
  298.                 long t = Calendar.getInstance().getTime().getTime();
  299.                 Calendar c = Calendar.getInstance();
  300.                 c.setTime(parse(date));
  301.                 long t1 = c.getTime().getTime();
  302.                 return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
  303.         }
  304.  
  305.         /**
  306.          * 按用户格式字符串距离今天的天数
  307.          *
  308.          * @param date
  309.          *            日期字符串
  310.          * @param format
  311.          *            日期格式
  312.          * @return
  313.          */
  314.         public static int countDays(String date, String format) {
  315.                 long t = Calendar.getInstance().getTime().getTime();
  316.                 Calendar c = Calendar.getInstance();
  317.                 c.setTime(parse(date, format));
  318.                 long t1 = c.getTime().getTime();
  319.                 return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
  320.         }
  321.  
  322.         public static void main(String[] args) {
  323.  
  324.                 System.out.println(DateUtil.getNow());
  325.         }
  326.  
  327. }

回复 "java 日期时间工具类"

这儿你可以回复上面这条便签

captcha