java date tools

The format the SimpleDateFormat new new = the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS");
Calendar Calendar.getInstance C = ();
1. past seven days
c.setTime (new new a Date ());
c.add (Calendar.DATE , - 7);
a Date d = c.getTime ();
String Day = format.format (d);
System.out.println ( "the last seven days:" + Day);
2. the past month
c.setTime (new Date ());
c.add (the Calendar.MONTH, -1);
a Date c.getTime m = ();
String Mon = format.format (m);
System.out.println ( "in the past month:" + mon) ;
3. Over the past three months
c.setTime (new new a Date ());
c.add (of the Calendar.MONTH, -3);
a Date M3 = c.getTime ();
String MON3 = format.format (M3);
System. out.println ( "the last three months:" + MON3);
4. past year
c.setTime (new Date ());
c.add (Calendar.YEAR, -1);
a Date c.getTime Y = ();
String = format.format year (Y);
System.out.println ( "last year:" + year);
5. The present 24 hour time  
the SimpleDateFormat the format the SimpleDateFormat new new = ( "the mM-dd-YYYY HH: mm: SS");  
String nowDate = format.format (new new a Date ());  
System.out.println (nowDate);  
6. the day start time of  
the SimpleDateFormat the format the SimpleDateFormat new new = ( "the MM-dd-YYYY");  
Calendar Calendar.getInstance C = ();  
String start = format.format (c.getTime ()) + "00:00:00";  
the System. out.println (start);  
deadline day 7  
the SimpleDateFormat the format the SimpleDateFormat new new = ( "the MM-dd-YYYY");  
Calendar Calendar Calendar.getInstance = ();  
End format.format = String (calendar.getTime ()) + "23:59:59";  
System.out.println (End);  
a week before the start time of the current time 8.  
SimpleDateFormat format = new SimpleDateFormat ( "yyyy- dd-the MM ");  
Calendar Calendar.getInstance C = ();  
c.add (Calendar.DAY_OF_MONTH, -6);  
String Start = format.format (c.getTime ()) +" 00:00:00 ";  
the System .out.println (start);  
prior to the date of the day 9 month  
the SimpleDateFormat the format the SimpleDateFormat new new = ( "the MM-dd-YYYY");  
Calendar Calendar.getInstance C = ();  
c.add (the Calendar.MONTH, -1) ; // get the previous month,    
String start = format.format (c.getTime ()) + "00:00:00";  
System.out.println (start);  
the year before the start time of the current time 10  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
Calendar c = Calendar.getInstance();  
c.add(Calendar.YEAR, -1); //年份减1   
String start =format.format(c.getTime())+" 00:00:00";  
System.out.println(start);  
11.当前时间的周一时间和周末时间 setfirstdayofweek()方法  
SimpleDateFormat format  = new SimpleDateFormat("YYYY-MM-dd ");  
Calendar c = Calendar.getInstance();  
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);  
String weekStart = format.format(c.getTime())+" 00:00:00";  
System.out.println(weekStart);  
Calendar ca = Calendar.getInstance();  
ca.setFirstDayOfWeek(Calendar.MONDAY);  
ca.set(Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek() + 6); // Sunday  
WEEKEND format.format = String (ca.getTime ()) + "23:59:59";  
System.out.println (WEEKEND);  
the first day and the last day of the month of the current time 12.  
SimpleDateFormat format = new SimpleDateFormat ( "the MM-dd-YYYY");  
Calendar Calendar.getInstance C = ();      
c.set (Calendar.DAY_OF_MONTH, 1); // set to 1, only the current date is the first day of the month   
String monthStart = format. the format (c.getTime ()) + "00:00:00";  
System.out.println (monthStart);  
Calendar Calendar.getInstance CA = ();      
ca.set (Calendar.DAY_OF_MONTH, ca.getActualMaximum (Calendar.DAY_OF_MONTH ));    
String monthEnd = format.format (ca.getTime ()) + "23:59:59";  
System.out.println (monthEnd);  
year profit 13. the time at which the start time  
SimpleDateFormat format = new SimpleDateFormat ( "  yyyy-MM-dd");  
C = Calendar.getInstance Calendar ();  
c.set (c.get (Calendar.YEAR), 0,. 1); // Start Date Time  
String yearStart = format.format (c.getTime () ) + "00:00 : 00 ";  
System.out.println (yearStart);  
Calendar CA = Calendar.getInstance ();  
ca.set (ca.get (Calendar.YEAR), 11, ca.getActualMaximum (Calendar.DAY_OF_MONTH)); // end date  
String yearEnd = format.format (ca.getTime ()) + "23:59:59";  
System.out.println (yearEnd);  
14. a time difference is calculated to obtain two time microsecond time difference
// get micro second-level time difference    
Long calendarEnd.getTimeInMillis Val = () - calendarBegin.getTimeInMillis ();    
// get in terms of number of days after    
Long day = Val / (1000 * 60 * 60 * 24);
15. a certain time obtaining Monday and Sunday
Calendar CAL = Calendar.getInstance ();
// postpone the n-number of weeks, one week, -1 forward one week later, two next week, and so on
int n = 1;
String monday;
cal.add(Calendar.DATE, n*7);
//想周几,这里就传几Calendar.MONDAY(TUESDAY...)
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
System.out.println(monday);

Guess you like

Origin www.cnblogs.com/riverone/p/12571646.html