• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
bigming
博客园    首页    新随笔    联系   管理    订阅  订阅

DateUtil

 1 public class DateUtil {
 2     public static final String FORMAT_DATE_COMPACT = "yyyyMMdd";
 3     public static final String FORMAT_DATE_NORMAL = "yyyy-MM-dd";
 4     public static final String FORMAT_DATETIME_COMPACT = "yyyyMMddHHmmss";
 5     public static final String FORMAT_DATETIME_COMPACT_DAY = "yyyyMMdd";
 6     public static final String FORMAT_DATETIME_NORMAL = "yyyy-MM-dd HH:mm:ss";
 7     public static final String FORMAT_TIMESTAMP_NORMAL = "yyyy-MM-dd HH:mm:ss.SSS";
 8 
 9     public static long getCurrentTimeMillis() {
10         return System.currentTimeMillis();
11     }
12 
13     public static Integer secondsBetween(Date start, Date end) {
14         DateTime startDt = new DateTime(start);
15         DateTime endDt = new DateTime(end);
16         int seconds = Seconds.secondsBetween(startDt, endDt).getSeconds();
17         return seconds;
18     }
19 
20     public static String formatNow(String formatPattern) {
21         return formatDate(getNow(), formatPattern);
22     }
23 
24     public static String formatDate(Date dt, String formatPattern) {
25         DateTime dateTime = new DateTime(dt);
26         return dateTime.toString(formatPattern);
27     }
28 
29     public static Date parseDate(String dateStr, String formatPattern) {
30         DateTimeFormatter fmt = DateTimeFormat.forPattern(formatPattern);
31         return fmt.parseDateTime(dateStr).toDate();
32     }
33 
34     public static Date getNow() {
35         return new Date();
36     }
37 
38     public static Date getYesterday() {
39         Date now = new Date();
40         DateTime dateTime = new DateTime(now);
41         return dateTime.minusDays(1).toDate();
42     }
43 
44     public static Date plusDays(Date date, int days) {
45         DateTime dateTime = new DateTime(date);
46         return dateTime.plusDays(days).toDate();
47     }
48 
49     public static Date minusDays(Date date, int days) {
50         DateTime dateTime = new DateTime(date);
51         return dateTime.minusDays(days).toDate();
52     }
53     public static Date plusHours(Date date, int hours) {
54         DateTime dateTime = new DateTime(date);
55         return dateTime.plusHours(hours).toDate();
56     }
57 
58     public static Date plusMinutes(Date date, int minutes) {
59         DateTime dateTime = new DateTime(date);
60         return dateTime.plusMinutes(minutes).toDate();
61     }
62 
63     public static Date minusMinutes(Date date, int minutes) {
64         DateTime dateTime = new DateTime(date);
65         return dateTime.minusMinutes(minutes).toDate();
66     }
67 
68     public static Date plusSeconds(Date date, int seconds) {
69         DateTime dateTime = new DateTime(date);
70         return dateTime.plusSeconds(seconds).toDate();
71     }
72 
73     public static Date minusSeconds(Date date, int seconds) {
74         DateTime dateTime = new DateTime(date);
75         return dateTime.minusSeconds(seconds).toDate();
76     }
77 
78     /**
79      * + 年
80      */
81     public static Date plusYears(Date date, int years) {
82         DateTime dateTime = new DateTime(date);
83         return dateTime.plusYears(years).toDate();
84     }
85 
86     /**
87      * - 年
88      */
89     public static Date minusYears(Date date, int years) {
90         DateTime dateTime = new DateTime(date);
91         return dateTime.minusYears(years).toDate();
92     }
93 }

 

posted @ 2019-01-29 16:37  bigming  阅读(130)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3