zoukankan      html  css  js  c++  java
  • 日期

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

    在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)、日期转换

    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

    • try {  
    •     //1、日期转字符串  
    •     Calendar calendar = Calendar.getInstance();  
    •     Date date = calendar.getTime();  
    •     String dateStringParse = sdf.format(date);  
    •     System.out.println(dateStringParse);  
    •     //2、字符串转日期  
    •     String dateString = "2017-12-20 14:02:08";  
    •     Date dateParse = sdf.parse(dateString);  
    •     System.out.println(dateParse);  
    • } catch (ParseException e) {  
    •     e.printStackTrace();  
    • }        


    注意:

    -创建 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+"";//获得小时;  
    223.             minute=v_int%3600/60+"";//获得小时;  
    224.             second=v_int%3600%60+"";//获得小时;  
    225.         }  
    226.         return (hour.length()>1?hour:"0"+hour)+":"+(minute.length()>1?minute:"0"+minute)+":"+(second.length()>1?second:"0"+second);  
    227.     }  
    228.     /**  
    229.      * 得到现在小时  
    230.      */  
    231.     public static String getHour() {  
    232.         Date currentTime = new Date();  
    233.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    234.         String dateString = formatter.format(currentTime);  
    235.         String hour;  
    236.         hour = dateString.substring(11, 13);  
    237.         return hour;  
    238.     }  
    239.   
    240.     /**  
    241.      * 得到现在分钟  
    242.      *  
    243.      * @return  
    244.      */  
    245.     public static String getTime() {  
    246.         Date currentTime = new Date();  
    247.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    248.         String dateString = formatter.format(currentTime);  
    249.         String min;  
    250.         min = dateString.substring(14, 16);  
    251.         return min;  
    252.     }  
    253.   
    254.     /**  
    255.      * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。  
    256.      *  
    257.      * @param sformat  
    258.      *            yyyyMMddhhmmss  
    259.      * @return  
    260.      */  
    261.     public static String getUserDate(String sformat) {  
    262.         Date currentTime = new Date();  
    263.         SimpleDateFormat formatter = new SimpleDateFormat(sformat);  
    264.         String dateString = formatter.format(currentTime);  
    265.         return dateString;  
    266.     }  
    267.   
    268.     /**  
    269.      * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟  
    270.      */  
    271.     public static String getTwoHour(String st1, String st2) {  
    272.         String[] kk = null;  
    273.         String[] jj = null;  
    274.         kk = st1.split(":");  
    275.         jj = st2.split(":");  
    276.         if (Integer.parseInt(kk[0]) Integer.parseInt(jj[0]))  
    277.             return "0";  
    278.         else {  
    279.             double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;  
    280.             double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;  
    281.             if ((y - u) > 0)  
    282.                 return y - u + "";  
    283.             else  
    284.                 return "0";  
    285.         }  
    286.     }  
    287.   
    288.     /**  
    289.      * 得到二个日期间的间隔天数  
    290.      */  
    291.     public static String getTwoDay(String sj1, String sj2) {  
    292.         long day = 0;  
    293.         try {  
    294.             java.util.Date date = formatter.parse(sj1);  
    295.             java.util.Date mydate = formatter.parse(sj2);  
    296.             day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
    297.         } catch (Exception e) {  
    298.             return "";  
    299.         }  
    300.         return day + "";  
    301.     }  
    302.   
    303.     /**  
    304.      * 时间前推或后推分钟,其中JJ表示分钟.  
    305.      */  
    306.     public static String getPreTime(String sj1, String jj) {  
    307.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    308.         String mydate1 = "";  
    309.         try {  
    310.             Date date1 = format.parse(sj1);  
    311.             long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;  
    312.             date1.setTime(Time * 1000);  
    313.             mydate1 = format.format(date1);  
    314.         } catch (Exception e) {  
    315.         }  
    316.         return mydate1;  
    317.     }  
    318.   
    319.     /**  
    320.      * 得到一个时间延后或前移几天的时间,nowdate(yyyy-mm-dd)为时间,delay为前移或后延的天数  
    321.      */  
    322.     public static String getNextDay(String nowdate, String delay) {  
    323.         try{  
    324.             String mdate = "";  
    325.             Date d = strToDate(nowdate);  
    326.             long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;  
    327.             d.setTime(myTime * 1000);  
    328.             mdate = formatter.format(d);  
    329.             return mdate;  
    330.         }catch(Exception e){  
    331.             return "";  
    332.         }  
    333.     }  
    334.     /**  
    335.      *  
    336.      * 功能:<br/> 距离现在几天的时间是多少  
    337.      * 获得一个时间字符串,格式为:yyyy-MM-dd HH:mm:ss  
    338.      * day  如果为整数,表示未来时间  
    339.      *      如果为负数,表示过去时间  
    340.      * @author Tony  
    341.      * @version 2016年11月29日 上午11:02:56 <br/>  
    342.      */  
    343.     public static String getFromNow(int day) {  
    344.         Date date = new Date();  
    345.         long dateTime = (date.getTime() / 1000) + day * 24 * 60 * 60;  
    346.         date.setTime(dateTime * 1000);  
    347.         return formatter2.format(date);  
    348.     }  
    349.     /**  
    350.      * 判断是否润年  
    351.      *  
    352.      * @param ddate  
    353.      * @return  
    354.      */  
    355.     public static boolean isLeapYear(String ddate) {  
    356.   
    357.         /**  
    358.          * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年  
    359.          * 3.能被4整除同时能被100整除则不是闰年  
    360.          */  
    361.         Date d = strToDate(ddate);  
    362.         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();  
    363.         gc.setTime(d);  
    364.         int year = gc.get(Calendar.YEAR);  
    365.         if ((year % 400) == 0)  
    366.             return true;  
    367.         else if ((year % 4) == 0) {  
    368.             if ((year % 100) == 0)  
    369.                 return false;  
    370.             else  
    371.                 return true;  
    372.         } else  
    373.             return false;  
    374.     }  
    375.   
    376.     /**  
    377.      * 返回美国时间格式 26 Apr 2006  
    378.      *  
    379.      * @param str  
    380.      * @return  
    381.      */  
    382.     public static String getEDate(String str) {  
    383.         ParsePosition pos = new ParsePosition(0);  
    384.         Date strtodate = formatter.parse(str, pos);  
    385.         String j = strtodate.toString();  
    386.         String[] k = j.split(" ");  
    387.         return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);  
    388.     }  
    389.   
    390.     /**  
    391.      * 获取一个月的最后一天  
    392.      *  
    393.      * @param dat  
    394.      * @return  
    395.      */  
    396.     public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd  
    397.         String str = dat.substring(0, 8);  
    398.         String month = dat.substring(5, 7);  
    399.         int mon = Integer.parseInt(month);  
    400.         if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {  
    401.             str += "31";  
    402.         } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {  
    403.             str += "30";  
    404.         } else {  
    405.             if (isLeapYear(dat)) {  
    406.                 str += "29";  
    407.             } else {  
    408.                 str += "28";  
    409.             }  
    410.         }  
    411.         return str;  
    412.     }  
    413.   
    414.     /**  
    415.      * 判断二个时间是否在同一个周  
    416.      *  
    417.      * @param date1  
    418.      * @param date2  
    419.      * @return  
    420.      */  
    421.     public static boolean isSameWeekDates(Date date1, Date date2) {  
    422.         Calendar cal1 = Calendar.getInstance();  
    423.         Calendar cal2 = Calendar.getInstance();  
    424.         cal1.setTime(date1);  
    425.         cal2.setTime(date2);  
    426.         int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);  
    427.         if (0 == subYear) {  
    428.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
    429.                 return true;  
    430.         } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {  
    431.             // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周  
    432.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
    433.                 return true;  
    434.         } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {  
    435.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
    436.                 return true;  
    437.         }  
    438.         return false;  
    439.     }  
    440.   
    441.     /**  
    442.      * 产生周序列,即得到当前时间所在的年度是第几周  
    443.      *  
    444.      * @return  
    445.      */  
    446.     public static String getSeqWeek() {  
    447.         Calendar c = Calendar.getInstance(Locale.CHINA);  
    448.         String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));  
    449.         if (week.length() == 1)  
    450.             week = "0" + week;  
    451.         String year = Integer.toString(c.get(Calendar.YEAR));  
    452.         return year + week;  
    453.     }  
    454.   
    455.     /**  
    456.      * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号  
    457.      *  
    458.      * @param sdate  
    459.      * @param num  
    460.      * @return  
    461.      */  
    462.     public static String getWeek(String sdate, String num) {  
    463.         // 再转换为时间  
    464.         Date dd = DateUtil.strToDate(sdate);  
    465.         Calendar c = Calendar.getInstance();  
    466.         c.setTime(dd);  
    467.         if (num.equals("1")) // 返回星期一所在的日期  
    468.             c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
    469.         else if (num.equals("2")) // 返回星期二所在的日期  
    470.             c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);  
    471.         else if (num.equals("3")) // 返回星期三所在的日期  
    472.             c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);  
    473.         else if (num.equals("4")) // 返回星期四所在的日期  
    474.             c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);  
    475.         else if (num.equals("5")) // 返回星期五所在的日期  
    476.             c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);  
    477.         else if (num.equals("6")) // 返回星期六所在的日期  
    478.             c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);  
    479.         else if (num.equals("0")) // 返回星期日所在的日期  
    480.             c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
    481.         return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
    482.     }  
    483.   
    484.     /**  
    485.      * 根据一个日期,返回是星期几的字符串  
    486.      *  
    487.      * @param sdate  
    488.      * @return  
    489.      */  
    490.     public static String getWeek(String sdate) {  
    491.         // 再转换为时间  
    492.         Date date = DateUtil.strToDate(sdate);  
    493.         Calendar c = Calendar.getInstance();  
    494.         c.setTime(date);  
    495.         // int hour=c.get(Calendar.DAY_OF_WEEK);  
    496.         // hour中存的就是星期几了,其范围 1~7  
    497.         // 1=星期日 7=星期六,其他类推  
    498.         return new SimpleDateFormat("EEEE").format(c.getTime());  
    499.     }  
    500.     public static String getWeekStr(String sdate){  
    501.         String str = "";  
    502.         str = DateUtil.getWeek(sdate);  
    503.         if("1".equals(str)){  
    504.             str = "星期日";  
    505.         }else if("2".equals(str)){  
    506.             str = "星期一";  
    507.         }else if("3".equals(str)){  
    508.             str = "星期二";  
    509.         }else if("4".equals(str)){  
    510.             str = "星期三";  
    511.         }else if("5".equals(str)){  
    512.             str = "星期四";  
    513.         }else if("6".equals(str)){  
    514.             str = "星期五";  
    515.         }else if("7".equals(str)){  
    516.             str = "星期六";  
    517.         }  
    518.         return str;  
    519.     }  
    520.   
    521.     /**  
    522.      * 两个时间之间的天数  
    523.      *  
    524.      * @param date1  
    525.      * @param date2  
    526.      * @return  
    527.      */  
    528.     public static long getDays(String date1, String date2) {  
    529.         if (date1 == null || date1.equals(""))  
    530.             return 0;  
    531.         if (date2 == null || date2.equals(""))  
    532.             return 0;  
    533.         // 转换为标准时间  
    534.         java.util.Date date = null;  
    535.         java.util.Date mydate = null;  
    536.         try {  
    537.             date = formatter.parse(date1);  
    538.             mydate = formatter.parse(date2);  
    539.         } catch (Exception e) {  
    540.         }  
    541.         long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
    542.         return day;  
    543.     }  
    544.   
    545.     /**  
    546.      * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间  
    547.      * 此函数返回该日历第一行星期日所在的日期  
    548.      *  
    549.      * @param sdate  
    550.      * @return  
    551.      */  
    552.     public static String getNowMonth(String sdate) {  
    553.         // 取该时间所在月的一号  
    554.         sdate = sdate.substring(0, 8) + "01";  
    555.   
    556.         // 得到这个月的1号是星期几  
    557.         Date date = DateUtil.strToDate(sdate);  
    558.         Calendar c = Calendar.getInstance();  
    559.         c.setTime(date);  
    560.         int u = c.get(Calendar.DAY_OF_WEEK);  
    561.         String newday = DateUtil.getNextDay(sdate, (1 - u) + "");  
    562.         return newday;  
    563.     }  
    564.   
    565.     /**  
    566.      * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数  
    567.      *  
    568.      * @param k  
    569.      *            表示是取几位随机数,可以自己定  
    570.      */  
    571.   
    572.     public static String getNo(int k) {  
    573.   
    574.         return getUserDate("yyyyMMddhhmmss") + getRandom(k);  
    575.     }  
    576.   
    577.     /**  
    578.      * 返回一个随机数  
    579.      *  
    580.      * @param i  
    581.      * @return  
    582.      */  
    583.     public static String getRandom(int i) {  
    584.         Random jjj = new Random();  
    585.         if (i == 0)  
    586.             return "";  
    587.         String jj = "";  
    588.         for (int k = 0; k i; k++) {  
    589.             jj = jj + jjj.nextInt(9);  
    590.         }  
    591.         return jj;  
    592.     }  
    593.   
    594.     public static boolean RightDate(String date) {  
    595.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    596.         if (date == null)  
    597.             return false;  
    598.         if (date.length() > 10) {  
    599.             sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    600.         } else {  
    601.             sdf = new SimpleDateFormat("yyyy-MM-dd");  
    602.         }  
    603.         try {  
    604.             sdf.parse(date);  
    605.         } catch (ParseException pe) {  
    606.             return false;  
    607.         }  
    608.         return true;  
    609.     }  
    610.   
    611.     /***************************************************************************  
    612.      * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1  
    613.      * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回  
    614.      **************************************************************************/  
    615.     public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {  
    616.         Date currentTime = new Date();  
    617.         String dateString = formatter.format(currentTime);  
    618.         String s_nd = dateString.substring(0, 4); // 年份  
    619.         String s_yf = dateString.substring(5, 7); // 月份  
    620.         String s_rq = dateString.substring(8, 10); // 日期  
    621.         String sreturn = "";  
    622.         //roc.util.MyChar mc = new roc.util.MyChar();  
    623.         //if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况  
    624.         if (sdate == null || sdate.equals("")){  
    625.             if (nd.equals("1")) {  
    626.                 sreturn = s_nd;  
    627.                 // 处理间隔符  
    628.                 if (format.equals("1"))  
    629.                     sreturn = sreturn + "年";  
    630.                 else if (format.equals("2"))  
    631.                     sreturn = sreturn + "-";  
    632.                 else if (format.equals("3"))  
    633.                     sreturn = sreturn + "/";  
    634.                 else if (format.equals("5"))  
    635.                     sreturn = sreturn + ".";  
    636.             }  
    637.             // 处理月份  
    638.             if (yf.equals("1")) {  
    639.                 sreturn = sreturn + s_yf;  
    640.                 if (format.equals("1"))  
    641.                     sreturn = sreturn + "月";  
    642.                 else if (format.equals("2"))  
    643.                     sreturn = sreturn + "-";  
    644.                 else if (format.equals("3"))  
    645.                     sreturn = sreturn + "/";  
    646.                 else if (format.equals("5"))  
    647.                     sreturn = sreturn + ".";  
    648.             }  
    649.             // 处理日期  
    650.             if (rq.equals("1")) {  
    651.                 sreturn = sreturn + s_rq;  
    652.                 if (format.equals("1"))  
    653.                     sreturn = sreturn + "日";  
    654.             }  
    655.         } else {  
    656.             // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式  
    657.             sdate = getOKDate(sdate);  
    658.             s_nd = sdate.substring(0, 4); // 年份  
    659.             s_yf = sdate.substring(5, 7); // 月份  
    660.             s_rq = sdate.substring(8, 10); // 日期  
    661.             if (nd.equals("1")) {  
    662.                 sreturn = s_nd;  
    663.                 // 处理间隔符  
    664.                 if (format.equals("1"))  
    665.                     sreturn = sreturn + "年";  
    666.                 else if (format.equals("2"))  
    667.                     sreturn = sreturn + "-";  
    668.                 else if (format.equals("3"))  
    669.                     sreturn = sreturn + "/";  
    670.                 else if (format.equals("5"))  
    671.                     sreturn = sreturn + ".";  
    672.             }  
    673.             // 处理月份  
    674.             if (yf.equals("1")) {  
    675.                 sreturn = sreturn + s_yf;  
    676.                 if (format.equals("1"))  
    677.                     sreturn = sreturn + "月";  
    678.                 else if (format.equals("2"))  
    679.                     sreturn = sreturn + "-";  
    680.                 else if (format.equals("3"))  
    681.                     sreturn = sreturn + "/";  
    682.                 else if (format.equals("5"))  
    683.                     sreturn = sreturn + ".";  
    684.             }  
    685.             // 处理日期  
    686.             if (rq.equals("1")) {  
    687.                 sreturn = sreturn + s_rq;  
    688.                 if (format.equals("1"))  
    689.                     sreturn = sreturn + "日";  
    690.             }  
    691.         }  
    692.         return sreturn;  
    693.     }  
    694.   
    695.     public static String getNextMonthDay(String sdate, int m) {  
    696.         sdate = getOKDate(sdate);  
    697.         int year = Integer.parseInt(sdate.substring(0, 4));  
    698.         int month = Integer.parseInt(sdate.substring(5, 7));  
    699.         month = month + m;  
    700.         if (month 0) {  
    701.             month = month + 12;  
    702.             year = year - 1;  
    703.         } else if (month > 12) {  
    704.             month = month - 12;  
    705.             year = year + 1;  
    706.         }  
    707.         String smonth = "";  
    708.         if (month 10)  
    709.             smonth = "0" + month;  
    710.         else  
    711.             smonth = "" + month;  
    712.         return year + "-" + smonth + "-10";  
    713.     }  
    714.   
    715.     /**  
    716.      *  
    717.      * 功能:<br/>  
    718.      *  
    719.      * @author Tony  
    720.      * @version 2015-3-31 上午09:29:31 <br/>  
    721.      */  
    722.     public static String getOKDate(String sdate) {  
    723.         if (sdate == null || sdate.equals(""))  
    724.             return getStringDateShort();  
    725.   
    726. //    if (!VeStr.Isdate(sdate)) {  
    727. //     sdate = getStringDateShort();  
    728. //    }  
    729. //    // 将“/”转换为“-”  
    730. //    sdate = VeStr.Replace(sdate, "/", "-");  
    731.         // 如果只有8位长度,则要进行转换  
    732.         if (sdate.length() == 8)  
    733.             sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);  
    734.         ParsePosition pos = new ParsePosition(0);  
    735.         Date strtodate = formatter.parse(sdate, pos);  
    736.         String dateString = formatter.format(strtodate);  
    737.         return dateString;  
    738.     }  
    739.     /**  
    740.      * 获取当前时间的前一天时间  
    741.      * @param cl  
    742.      * @return  
    743.      */  
    744.     private static String getBeforeDay(Calendar cl){  
    745.         //使用roll方法进行向前回滚  
    746.         //cl.roll(Calendar.DATE, -1);  
    747.         //使用set方法直接进行设置  
    748.         // int day = cl.get(Calendar.DATE);  
    749.         cl.add(Calendar.DATE, -1);  
    750.         return formatter.format(cl.getTime());  
    751.     }  
    752.   
    753.     /**  
    754.      * 获取当前时间的后一天时间  
    755.      * @param cl  
    756.      * @return  
    757.      */  
    758.     private static String getAfterDay(Calendar cl){  
    759.         //使用roll方法进行回滚到后一天的时间  
    760.         //cl.roll(Calendar.DATE, 1);  
    761.         //使用set方法直接设置时间值  
    762.         //int day = cl.get(Calendar.DATE);  
    763.         cl.add(Calendar.DATE, 1);  
    764.         return formatter.format(cl.getTime());  
    765.     }  
    766.   
    767.     private static String getDateAMPM(){  
    768.         GregorianCalendar ca = new GregorianCalendar();  
    769.         //结果为“0”是上午     结果为“1”是下午  
    770.         int i=ca.get(GregorianCalendar.AM_PM);  
    771.         return i==0?"AM":"PM";  
    772.     }  
    773.     private static int compareToDate(String date1,String date2){  
    774.         return date1.compareTo(date2);  
    775.     }  
    776.     private static int compareToDateString(String date1,String date2){  
    777.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    778.         int i=0;  
    779.         try {  
    780.             long ldate1=formatter.parse(date1).getTime();  
    781.             long ldate2=formatter.parse(date2).getTime();  
    782.             if(ldate1>ldate2){  
    783.                 i=1;  
    784.             }else if(ldate1==ldate2){  
    785.                 i=0;  
    786.             }else{  
    787.                 i=-1;  
    788.             }  
    789.   
    790.         } catch (ParseException e) {  
    791.             e.printStackTrace();  
    792.         }  
    793.         return i;  
    794.     }  
    795.   
    796.     public static String[] getFiveDate(){  
    797.         String[] dates=new String[2];  
    798.         Calendar calendar = Calendar.getInstance();  
    799.         calendar.setTime(new Date());  
    800.         String five=" 05:00:00";  
    801.   
    802.         if(getDateAMPM().equals("AM")&&compareToDateString(getStringDate(),getStringDateShort()+five)==-1){  
    803.             dates[0]=getBeforeDay(calendar)+five;  
    804.             dates[1]=getStringDateShort()+five;  
    805.         }else{  
    806.             dates[0]=getStringDateShort()+five;  
    807.             dates[1]=getAfterDay(calendar)+five;  
    808.         }  
    809.   
    810.         return dates;  
    811.     }  
    812.     public static String getFiveDate2(){  
    813.         Calendar calendar = Calendar.getInstance();  
    814.         calendar.setTime(new Date());  
    815.         String five=" 05:00:00";  
    816.         String reStr="";  
    817.         if(getDateAMPM().equals("AM")&&compareToDateString(getStringDate(),getStringDateShort()+five)==-1){  
    818.             reStr=getBeforeDay(calendar);  
    819.         }else{  
    820.             reStr=getStringDateShort();  
    821.         }  
    822.         return reStr;  
    823.     }  

     
  • 相关阅读:
    钉钉服务器端SDK PHP版
    戒不掉
    亲历钓鱼网站
    我整理的PHP 7.0主要新特性
    JavaScript中JSON的处理心得
    X3D中Profile如何翻译
    你可以写什么?
    Gitblit中采用Ticket模式进行协作开发
    理解JavaScript中的事件处理 阻止冒泡event.stopPropagation();
    测试Remoting三种信道Http,Tcp,Ipc和Web Service的访问速度 (转)
  • 原文地址:https://www.cnblogs.com/xiaoniuniu886/p/9959757.html
Copyright © 2011-2022 走看看