JAVA中关于获取时间(日期)的总结大全 - Go语言中文社区

JAVA中关于获取时间(日期)的总结大全


在开发过程中,少不了与时间打交道,比如根据两个日期得出相差的时分秒,时间加减,时间累加,前5分钟,前一个月,前一年,等等...


而我最近开发和时间操作的比较频繁,所以记录下,和时间操作有关的代码。


在JAVA中有六个与时间有关的类:

  1. java.util.Date  
  2. java.sql.Date    
  3. java.sql.Time    
  4. java.sql.Timestamp  
  5. java.text.SimpleDateFormat  
  6. java.util.Calendar  


常用的也就是下面的几个:

  1. Date:          getTime() 、setTime()  
  2. DateFormat:           getInstance()、getDateInstance()、getDateTimeInstance()、getTimeInstance()  
  3. SimpleDateFormate:    Formate(Date)、 parse(String s)  
  4. Calendar:        getInstance()、set() 、get()、getActualMaximum()、add()、gettime()、setTime(Date)  

下面分别对他们介绍下:


(1)、java.util.Date 


java.util.Date 是java.sqlDate,Time,Timestamp的父类,Java中的时间使用标准类库的java.util.Date,其表示特定的瞬间,精确到毫秒。是用距离一个固定时间点的毫秒数(可正可负,long类型)表达一个特定的时间点。从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和分析日期字符串。因为Date的设计具有"千年虫"以及"时区"的问题,所以Date中的大部分方法已经不建议使用了,它们都被java.util.Calendar类所取代


(2)、java.text.DateFormat(抽象类) 


       DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并分析日期或时间。日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(日期 -> 文本)、分析(文本-> 日期)和标准化。将日期表示为 Date 对象,或者表示为从 GMT(格林尼治标准时间)1970 年1 月 1 日 00:00:00 这一刻开始的毫秒数。 不过DateFormat的格式化Date的功能有限,没有SimpleDateFormat强大;但DateFormat是SimpleDateFormat的父类。

(3)、java.text.SimpleDateFormat  (DateFormat的直接子类) 


 SimpleDateFormat 是一个以与语言环境相关的方式来格式化和分析日期的具体类。


       SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过 DateFormat 中的 getTimeInstance、getDateInstance 或 getDateTimeInstance 来新的创建日期-时间格式化程序。 


日期格式字符串如下:




常见的转换有两种:


将Date格式化为String    String format(Date d)
将String解析为Date    Date   parse(String s)


(4)、java.util.Calendar(抽象类) 


       java.util.Calendar 类用于封装日历信息,其主要作用在于其方法可以对时间分量进行运算


       Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。

 
       与其他语言环境敏感类一样,Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。Calendar 的 getInstance 方法返回一个 Calendar 对象,其日历字段已由当前日期和时间初始化。 
  


二、示例


大概可以分为以下五大类:


(A)、日期取值


1.1、获取当前系统时间(毫秒数)

  1. //方式一  
  2. Date date = new Date();  
  3. System.out.println(date.getTime());  
  4.   
  5. //方式二  
  6. System.out.println(System.currentTimeMillis());  
  7.   
  8. //转换成指定的格式  
  9. String current = df.format(System.currentTimeMillis());  
  10. System.out.println(current);         
  11.   
  12.   
  13. 输出如下:  
  14. 1513749728479  
  15. 1513749728480  
  16. 2017-12-20 14:02:08   

(B)、日期转换


2.1、日期转字符串、字符串转日期

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. try {  
  3.     //1、日期转字符串  
  4.     Calendar calendar = Calendar.getInstance();  
  5.     Date date = calendar.getTime();  
  6.     String dateStringParse = sdf.format(date);  
  7.     System.out.println(dateStringParse);  
  8.     //2、字符串转日期  
  9.     String dateString = "2017-12-20 14:02:08";  
  10.     Date dateParse = sdf.parse(dateString);  
  11.     System.out.println(dateParse);  
  12. } catch (ParseException e) {  
  13.     e.printStackTrace();  
  14. }        


注意:

-创建 SimpleDateFormat 对象时必须指定转换格式。
-转换格式区分大小写,yyyy 代表年份,MM 代表月份,dd 代表日期,HH 代表 24 进制的小时,hh 代表 12 进制的小时,mm 代表分钟,ss 代表秒。


2.2、将日期转换成中文年月日时分秒

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
  2. try {  
  3.     Date date = new Date();  
  4.     String dateStringParse = sdf.format(date);  
  5.     System.out.println(dateStringParse);  
  6. } catch (Exception e) {  
  7.     e.printStackTrace();  
  8. }  


2.3、将指定日期转换成带周的格式

  1. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. Date d1 = df.parse("2017-12-20 12:19:19");  
  3. //指定日期  
  4. Calendar cal = df.getCalendar();  
  5. //当前时间  
  6. Calendar cas = Calendar.getInstance();  
  7. int year = cal.get(Calendar.YEAR);//获取年份  
  8. int month=cal.get(Calendar.MONTH);//获取月份  
  9. int day=cal.get(Calendar.DATE);//获取日  
  10. int hour=cal.get(Calendar.HOUR);//小时  
  11. int minute=cal.get(Calendar.MINUTE);//分  
  12. int second=cal.get(Calendar.SECOND);//秒  
  13. int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天  
  14. System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒     星期"+WeekOfYear);  

2.4、获取当前时间显示,上午,下午

  1. Date date = new Date();  
  2. DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日  
  3. System.out.println(df1.format(date));  
  4. DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒  
  5. System.out.println(df2.format(date));  
  6. DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒  
  7. System.out.println("只显示出时分秒:"+df3.format(date));  
  8. DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)  
  9. System.out.println("显示日期,周,上下午,时间(精确到秒):"+df4.format(date));  
  10. DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒)  
  11. System.out.println("显示日期,上下午,时间(精确到秒):"+df5.format(date));  
  12. DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分)  
  13. System.out.println("显示日期,上下午,时间(精确到分):"+df6.format(date));  
  14. DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)  
  15. System.out.println("显示日期,时间(精确到分):"+df7.format(date));  


2.5、时间秒转化为多少天小时分秒

  1. /**    
  2.      * 秒转化为天小时分秒字符串    
  3.      *    
  4.      * @param seconds    
  5.      * @return String    
  6.      */      
  7.     public static String formatSeconds(long seconds) {      
  8.         String timeStr = seconds + "秒";      
  9.         if (seconds > 60) {      
  10.             long second = seconds % 60;      
  11.             long min = seconds / 60;      
  12.             timeStr = min + "分" + second + "秒";      
  13.             if (min > 60) {      
  14.                 min = (seconds / 60) % 60;      
  15.                 long hour = (seconds / 60) / 60;      
  16.                 timeStr = hour + "小时" + min + "分" + second + "秒";      
  17.                 if (hour > 24) {      
  18.                     hour = ((seconds / 60) / 60) % 24;      
  19.                     long day = (((seconds / 60) / 60) / 24);      
  20.                     timeStr = day + "天" + hour + "小时" + min + "分" + second + "秒";      
  21.                 }      
  22.             }      
  23.         }      
  24.         return timeStr;      
  25.  }      



(C)、设置时间


Calendar.getInstance().getTime()即可获取一个Date对象
Calendar.getInstance().add(时间的一个部分,正数代表加,负数代表减)


推荐 使用java.util.Calendar类来进行操作,因为java.util.Date类很多方法都过时了,Calendar 类有很多重载的设置时间的方法,可以针对于某一项进行设置,也可以同时进行很多设置

  1. set(int field, int value) 将给定的日历字段设置为给定值。  
  2. void    set(int year, int month, int date) 设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。  
  3. void    set(int year, int month, int date, int hourOfDay, int minute) 设置日历字段 YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值。  
  4. void    set(int year, int month, int date, int hourOfDay, int minute, int second) 设置字段 YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值。  

第一个就是给特定的字段设值,后面三个就是针对多个字段赋值


(D)、日期加减


通常来说,我们会对日期做两种加减操作:

一、以某个日期为基准,计算其几天前/后、几年前/后,或者其他时间单位前后的日期


A、获取当前时间的前一年时间

  1. //根据现在时间计算  
  2. Calendar now = Calendar.getInstance();  
  3. now.add(Calendar.YEAR, 1); //现在时间是1年后  
  4. print(now);  
  5. now.add(Calendar.YEAR, -1); //现在时间是1年前  
  6. print(now);  
  7.   
  8. //根据某个特定的时间 date (Date 型) 计算  
  9. Calendar specialDate = Calendar.getInstance();  
  10. specialDate.setTime(new Date()); //注意在此处将 specialDate 的值改为特定日期  
  11. specialDate.add(Calendar.YEAR, 1); //特定时间的1年后  
  12. print(specialDate);  
  13. specialDate.add(Calendar.YEAR, -1); //特定时间的1年前  
  14. print(specialDate);   

打印Calendar的方法为:

  1. public void print(Calendar specialDate){  
  2.     System.out.println(specialDate.get(Calendar.YEAR)+"年"+(specialDate.get(Calendar.MONTH)+1)+  
  3.             "月"+specialDate.get(Calendar.DAY_OF_MONTH)+"日"+specialDate.get(Calendar.HOUR_OF_DAY)+  
  4.             ":"+specialDate.get(Calendar.MINUTE)+":"+specialDate.get(Calendar.SECOND));  
  5. }        

使用了 Calendar 对象的 add 方法,可以更改 Calendar.YEAR 为任意时间单位字段,完成各种时间单位下的日期计算。


B、根据时间然后对小时,分钟,秒数进行相加

  1. //取当前的时分-30  
  2. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
  3. Calendar nowTime2 = Calendar.getInstance();  
  4. nowTime2.add(Calendar.MINUTE, -30);//30分钟前的时间  
  5. String currentTime = df.format(nowTime2.getTime());  
  6. System.out.println("30分钟前的时间" + currentTime);  
  7.   
  8. //取当前小时减一个小时  
  9. Calendar calendar = Calendar.getInstance();  
  10. calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);  
  11. SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH");  
  12. System.out.println("一个小时前的时间:" + dff.format(calendar.getTime()));  
  13. System.out.println("当前的时间:" + dff.format(new Date()));  
  14.   
  15. //取当前的时分+5  
  16. Calendar nowTime = Calendar.getInstance();  
  17. nowTime.add(Calendar.MINUTE, 5);  
  18. String currentTimes = df.format(nowTime.getTime());  
  19. System.out.println("当前时间加5分钟" +currentTimes);  
  20.   
  21. //前一天的时间  
  22. Date dNow = new Date();   //当前时间  
  23. Calendar calendars = Calendar.getInstance(); //得到日历  
  24. calendars.setTime(dNow);//把当前时间赋给日历  
  25. calendars.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天  
  26. Date dBefore = calendars.getTime();   //得到前一天的时间  
  27.   
  28. String defaultStartDate = df.format(dBefore);    //格式化前一天  
  29. String defaultEndDate = df.format(dNow); //格式化当前时间  
  30.   
  31. System.out.println("前一天的时间是:" + defaultStartDate);  
  32. System.out.println("生成的时间是:" + defaultEndDate);  

C、对时分秒进行累计,比如说视频通话,第一次,通了10分钟,第二次就得把上次的时间进行累计。

  1. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. //上次挂断电话时间  
  3. Date d1 = df.parse("2017-12-20 12:19:19");  
  4. //这次通话时长  
  5. Date d2 = df.parse("2017-12-20 11:40:34");  
  6. long nd = 1000 * 24 * 60 * 60;  
  7. long nh = 1000 * 60 * 60;  
  8. long nm = 1000 * 60;  
  9. long ns = 1000;  
  10. // 获得两个时间的毫秒时间差异  
  11. long diff = d1.getTime() - d2.getTime();  
  12. // 计算差多少天  
  13. long day = diff / nd;  
  14. // 计算差多少小时  
  15. long hour = diff % nd / nh;  
  16. // 计算差多少分钟  
  17. long min = diff % nd % nh / nm;  
  18. // 计算差多少秒/输出结果  
  19. long sec = diff % nd % nh % nm / ns;  
  20. //根据上次挂断电话时间然后得出这次通话时长  
  21. System.out.println(day + "天" + hour + "小时" + min + "分钟"+ sec + "秒");  
  22.   
  23. //累计通话时长  
  24. String cur = "10:15:12";  
  25. SimpleDateFormat myFormatter = new SimpleDateFormat("HH:mm:ss");  
  26. Calendar cal = Calendar.getInstance();  
  27. cal.setTime(myFormatter.parse(cur));  
  28. int shi = (int)cal.get(Calendar.HOUR_OF_DAY)+(int)hour;  
  29. int fendo = (int)cal.get(Calendar.MINUTE)+(int)min;  
  30. int miao = (int)cal.get(Calendar.SECOND)+(int)sec;  
  31.   
  32. //以下算法有些问题  
  33. if(miao>60){  
  34.     miao=00;  
  35.     if(fendo>60){  
  36.         fendo = 00;  
  37.         shi = shi+1;  
  38.     }else {  
  39.         fendo = fendo + 1;  
  40.     }  
  41. }  
  42. System.out.println(shi+":"+fendo+":"+miao);  


二、计算两个时间的间隔


A、例如计算 2017 年 1 月 1 日距离现在有多少天。  

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. String dateString = "2017-01-01 11:11:11";  
  3. Calendar calendar = Calendar.getInstance();  
  4. long nowDate = calendar.getTime().getTime(); //Date.getTime() 获得毫秒型日期  
  5. try {  
  6.     long specialDate = sdf.parse(dateString).getTime();  
  7.     long betweenDate = (specialDate - nowDate) / (1000 * 60 * 60 * 24); //计算间隔多少天,则除以毫秒到天的转换公式  
  8.     System.out.print(betweenDate);  
  9. } catch (ParseException e) {  
  10.     e.printStackTrace();  
  11. }  


B、求出两个日期相差多少小时,分钟,毫秒

  1. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. Date d1 = df.parse("2017-12-20 12:19:19");  
  3. Date d2 = df.parse("2017-12-20 11:40:34");  
  4. long nd = 1000 * 24 * 60 * 60;  
  5. long nh = 1000 * 60 * 60;  
  6. long nm = 1000 * 60;  
  7. long ns = 1000;  
  8. // 获得两个时间的毫秒时间差异  
  9. long diff = d1.getTime() - d2.getTime();  
  10. // 计算差多少天  
  11. long day = diff / nd;  
  12. // 计算差多少小时  
  13. long hour = diff % nd / nh;  
  14. // 计算差多少分钟  
  15. long min = diff % nd % nh / nm;  
  16. // 计算差多少秒//输出结果  
  17. long sec = diff % nd % nh % nm / ns;  
  18. System.out.println(day + "天" + hour + "小时" + min + "分钟"+ sec + "秒");  


(E)、日期比较


日期比较一般有两种方法,对于 java.util.Date 或者 java.util.Calendar 都是通用的。一种是通过 after() 与 before() 方法进行比较,一种是通过 compareTo() 方法进行比较。

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. String dateString_01 = "2017-12-20 11:11:11";  
  3. String dateString_02 = "2017-12-21 11:11:11";  
  4. try {  
  5.     Date date_01 = sdf.parse(dateString_01);  
  6.     Date date_02 = sdf.parse(dateString_02);  
  7.     System.out.println(date_01.before(date_02)); //true,当 date_01 小于 date_02 时,为 true,否则为 false  
  8.     System.out.println(date_02.after(date_01)); //true,当 date_02 大于 date_01 时,为 true,否则为 false  
  9.     System.out.println(date_01.compareTo(date_02)); //-1,当 date_01 小于 date_02 时,为 -1  
  10.     System.out.println(date_02.compareTo(date_01)); //1,当 date_02 大于 date_01 时,为 1  
  11.     System.out.println(date_02.compareTo(date_02)); //0,当两个日期相等时,为 0  
  12. } catch (ParseException e) {  
  13.     e.printStackTrace();  
  14. }  


三、工具类


网上找的一个工具类:
  1. /**  
  2.  * projectName: xxxx  
  3.  * fileName: DateUtil.java  
  4.  * packageName: test  
  5.  * date: 2017-12-20 15:36  
  6.  * copyright(c) 2017-2020 xxx公司  
  7.  */  
  8. package test;  
  9.   
  10. import java.sql.Timestamp;  
  11. import java.text.ParseException;  
  12. import java.text.ParsePosition;  
  13. import java.text.SimpleDateFormat;  
  14. import java.util.*;  
  15.   
  16. /**  
  17.  * @version: V1.0  
  18.  * @author: fendo  
  19.  * @className: DateUtil  
  20.  * @packageName: test  
  21.  * @description: 时间操作工具类  
  22.  * @data: 2017-12-20 15:36  
  23.  **/  
  24. public class DateUtil {  
  25.   
  26.     public static String pattern="yyyy-MM-dd";  
  27.     public static SimpleDateFormat formatter = new SimpleDateFormat(pattern);  
  28.     public static SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  29.   
  30.     /**  
  31.      * 获取现在时间  
  32.      *  
  33.      * @return 返回时间类型 yyyy-MM-dd HH:mm:ss  
  34.      */  
  35.     public static Date getNowDate() {  
  36.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  37.         String dateString = formatter.format(new Date());  
  38.         ParsePosition pos = new ParsePosition(8);  
  39.         Date currentTime_2 = formatter.parse(dateString, pos);  
  40.         return currentTime_2;  
  41.     }  
  42.   
  43.     /**  
  44.      * 获取现在时间  
  45.      *  
  46.      * @return返回短时间格式 yyyy-MM-dd  
  47.      */  
  48.     public static Date getNowDateShort() {  
  49.         String dateString = formatter.format(new Date());  
  50.         ParsePosition pos = new ParsePosition(8);  
  51.         Date currentTime_2 = formatter.parse(dateString, pos);  
  52.         return currentTime_2;  
  53.     }  
  54.   
  55.     /**  
  56.      * 获取现在时间  
  57.      *  
  58.      * @return返回字符串格式 yyyy-MM-dd HH:mm:ss  
  59.      */  
  60.     public static String getStringDate() {  
  61.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  62.         String dateString = formatter.format(new Date());  
  63.         return dateString;  
  64.     }  
  65.     /**  
  66.      * 获取现在时间  
  67.      * @return返回字符串格式 yyyyMMddHHmmss  
  68.      */  
  69.     public static String getStringAllDate() {  
  70.         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");  
  71.         String dateString = formatter.format(new Date());  
  72.         return dateString;  
  73.     }  
  74.     /**  
  75.      * 获取现在时间  
  76.      *  
  77.      * @return 返回短时间字符串格式yyyy-MM-dd  
  78.      */  
  79.     public static String getStringDateShort() {  
  80.         String dateString = formatter.format( new Date());  
  81.         return dateString;  
  82.     }  
  83.   
  84.     /**  
  85.      * 获取时间 小时:分;秒 HH:mm:ss  
  86.      *  
  87.      * @return  
  88.      */  
  89.     public static String getTimeShort() {  
  90.         SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
  91.         String dateString = formatter.format(new Date());  
  92.         return dateString;  
  93.     }  
  94.   
  95.     /**  
  96.      * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss  
  97.      *  
  98.      * @param strDate  
  99.      * @return  
  100.      */  
  101.     public static Date strToDateLong(String strDate) {  
  102.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  103.         ParsePosition pos = new ParsePosition(0);  
  104.         Date strtodate = formatter.parse(strDate, pos);  
  105.         return strtodate;  
  106.     }  
  107.   
  108.     /**  
  109.      * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  
  110.      *  
  111.      * @param dateDate  
  112.      * @return  
  113.      */  
  114.     public static String dateToStrLong(java.util.Date dateDate) {  
  115.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  116.         String dateString = formatter.format(dateDate);  
  117.         return dateString;  
  118.     }  
  119.   
  120.     /**  
  121.      * 将短时间格式时间转换为字符串 yyyy-MM-dd  
  122.      *  
  123.      * @param dateDate  
  124.      * @return  
  125.      */  
  126.     public static String dateToStr(Date dateDate) {  
  127.         String dateString = formatter.format(dateDate);  
  128.         return dateString;  
  129.     }  
  130.   
  131.   
  132.     /**  
  133.      * 将短时间格式字符串转换为时间 yyyy-MM-dd  
  134.      *  
  135.      * @param strDate  
  136.      * @return  
  137.      */  
  138.     public static Date strToDate(String strDate) {  
  139.         ParsePosition pos = new ParsePosition(0);  
  140.         Date strtodate = formatter.parse(strDate, pos);  
  141.         return strtodate;  
  142.     }  
  143.   
  144.     /**  
  145.      * 将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss  
  146.      *  
  147.      * @param strDate  
  148.      * @return  
  149.      */  
  150.     public static Timestamp strToDateSql(String strDate) {  
  151.         ParsePosition pos = new ParsePosition(0);  
  152.         Date strtodate = formatter2.parse(strDate, pos);  
  153.         return new Timestamp(strtodate.getTime());  
  154.     }  
  155.     /**  
  156.      * 得到现在时间  
  157.      *  
  158.      * @return  
  159.      */  
  160.     public static Date getNow() {  
  161.         Date currentTime = new Date();  
  162.         return currentTime;  
  163.     }  
  164.   
  165.     /**  
  166.      * 提取一个月中的最后一天  
  167.      *  
  168.      * @param day  
  169.      * @return  
  170.      */  
  171.     public static Date getLastDate(long day) {  
  172.         Date date = new Date();  
  173.         long date_3_hm = date.getTime() - 3600000 * 34 * day;  
  174.         Date date_3_hm_date = new Date(date_3_hm);  
  175.         return date_3_hm_date;  
  176.     }  
  177.   
  178.     /**  
  179.      * 得到现在时间  
  180.      *  
  181.      * @return 字符串 yyyyMMdd HHmmss  
  182.      */  
  183.     public static String getStringToday() {  
  184.         Date currentTime = new Date();  
  185.         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");  
  186.         String dateString = formatter.format(currentTime);  
  187.         return dateString;  
  188.     }  
  189.     /**  
  190.      *  
  191.      * 功能:<br/>  
  192.      *  
  193.      * @author Tony  
  194.      * @version 2016年12月16日 下午4:41:51 <br/>  
  195.      */  
  196.     public static String getTodayShort() {  
  197.         Date currentTime = new Date();  
  198.         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");  
  199.         String dateString = formatter.format(currentTime);  
  200.         return dateString;  
  201.     }  
  202.     /**  
  203.      *  
  204.      * @Description: 输入一个整数类型的字符串,然后转换成时分秒的形式输出  
  205.      *    例如:输入568  
  206.      *          返回结果为:00:09:28  
  207.      *          输入null或者“”  
  208.      *          返回结果为:00:00:00  
  209.      * @param @param value  
  210.      * @param @return  
  211.      * @return String  
  212.      * @throws  
  213.      * @author Tony 鬼手卡卡  
  214.      * @date 2016-4-20  
  215.      */  
  216.     public static String getHHMMSS(String value){  
  217.         String hour="00";  
  218.         String minute="00";  
  219.         String second="00";  
  220.         if(value!=null&&!value.trim().equals("")){  
  221.             int v_int=Integer.valueOf(value);  
  222.             hour=v_int/3600+
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_37539378/article/details/78889992
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 15:21:30
  • 阅读 ( 1059 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢