Java最近时间处理类

  1 /**
  2  * 项目名称:dream_util
  3  * 项目包名:org.songbai.fore.utils.date
  4  * 创建时间:2017年8月23日下午5:06:54
  5  * 创建者:Administrator-宋发元
  6  * 创建地点:
  7  */
  8 package org.songbai.fore.utils.date;
  9 
 10 import java.text.SimpleDateFormat;
 11 import java.util.Calendar;
 12 import java.util.Date;
 13 
 14 /**
 15  * 描述:最近时间处理类
 16  * @author songfayuan
 17  * 2017年8月23日下午5:06:54
 18  */
 19 public class BeforeTimeUtil {
 20     
 21     /**
 22      * 描述:昨天
 23      * @return
 24      * @author songfayuan
 25      * 2017年8月23日下午6:11:36
 26      */
 27     public static String beforeYesterdayDay() {
 28         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
 29         Calendar c = Calendar.getInstance();
 30         c.setTime(new Date());
 31         c.add(Calendar.DATE, - 1);
 32         Date d = c.getTime();
 33         String day = format.format(d);
 34         //System.out.println("昨天:"+day);
 35         return day;
 36     }
 37 
 38     /**
 39      * 描述:过去5天
 40      * @return
 41      * @author songfayuan
 42      * 2017年8月23日下午6:11:27
 43      */
 44     public static String beforeFiveDay() {
 45         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
 46         Calendar c = Calendar.getInstance();
 47         c.setTime(new Date());
 48         c.add(Calendar.DATE, - 5);
 49         Date d = c.getTime();
 50         String day = format.format(d);
 51         //System.out.println("过去5天:"+day);
 52         return day;
 53     }
 54     
 55     /**
 56      * 描述:过去10天
 57      * @return
 58      * @author songfayuan
 59      * 2017年8月23日下午6:11:20
 60      */
 61     public static String beforeTenDay() {
 62         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
 63         Calendar c = Calendar.getInstance();
 64         c.setTime(new Date());
 65         c.add(Calendar.DATE, - 10);
 66         Date d = c.getTime();
 67         String day = format.format(d);
 68         //System.out.println("过去10天:"+day);
 69         return day;
 70     }
 71     
 72     /**
 73      * 描述:过去15天
 74      * @return
 75      * @author songfayuan
 76      * 2017年8月23日下午6:11:14
 77      */
 78     public static String beforeFifteenDay() {
 79         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
 80         Calendar c = Calendar.getInstance();
 81         c.setTime(new Date());
 82         c.add(Calendar.DATE, - 15);
 83         Date d = c.getTime();
 84         String day = format.format(d);
 85         //System.out.println("过去15天:"+day);
 86         return day;
 87     }
 88     
 89     /**
 90      * 描述:过去一月
 91      * @return
 92      * @author songfayuan
 93      * 2017年8月23日下午6:11:08
 94      */
 95     public static String beforeOneMonthDay() {
 96         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
 97         Calendar c = Calendar.getInstance();
 98         c.setTime(new Date());
 99         c.add(Calendar.MONTH, -1);
100         Date m = c.getTime();
101         String mon = format.format(m);
102         //System.out.println("过去一个月:"+mon);
103         return mon;
104     }
105     
106     /**
107      * 描述:过去三个月
108      * @return
109      * @author songfayuan
110      * 2017年8月23日下午6:11:02
111      */
112     public static String beforeThreeMonthDay() {
113         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
114         Calendar c = Calendar.getInstance();
115         c.setTime(new Date());
116         c.add(Calendar.MONTH, -3);
117         Date m3 = c.getTime();
118         String mon3 = format.format(m3);
119         //System.out.println("过去三个月:"+mon3);
120         return mon3;
121     }
122     
123     /**
124      * 描述:过去一年
125      * @return
126      * @author songfayuan
127      * 2017年8月23日下午6:10:54
128      */
129     public static String beforeOneYearDay() {
130         SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
131         Calendar c = Calendar.getInstance();
132         c.setTime(new Date());
133         c.add(Calendar.YEAR, -1);
134         Date y = c.getTime();
135         String year = format.format(y);
136         //System.out.println("过去一年:"+year);
137         return year;
138     }
139     
140     public static void main(String[] args) {
141         //yyyy-MM-dd HH:mm:ss
142         String day = beforeFiveDay();
143         String day2 = beforeTenDay();
144         
145         System.out.println("***********比较结果************");
146         int res=day.compareTo(day2);
147         if(res>0)
148             System.out.println("day>day2");
149         else if(res==0)
150             System.out.println("day=day2");
151         else
152             System.out.println("day<day2");
153         
154     }
155     
156 }

 

转载于:https://www.cnblogs.com/songfayuan/articles/7421881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值