Java handles the summary of date and time methods

  • 2020-05-24 05:38:54
  • OfStack

1. java. util Calendar is introduced

The Calendar class is an abstract class that provides methods for converting a particular instant to a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and other methods for manipulating a calendar field, such as getting the date for the next week. An instant can be represented by a value of milliseconds, which is the offset from the epoch (00:00:000 GMT on January 1, 1970, the Gregorian calendar).

2. Simple example


//  Output the date by formatting it 
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");

Calendar cal = Calendar.getInstance();//  Take the current date. 
System.out.println("Today is:" + format.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);//  Takes before the current date 1 day .
System.out.println("yesterday is:" + format.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, +1);//  After the current date 1 day .
System.out.println("nextday is:" + format.format(cal.getTime()));

or


java.util.Date today=new java.util.Date(); 
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Today is "+dateFormat.format(today));
System.out.println("Now is "+dateTimeFormat.format(today));

2. Construct a specific time


java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 

Calendar calendar = new GregorianCalendar(2007, 11, 25,0,0,0); 
Date date = calendar.getTime(); 
System.out.println("2007 Christmas is:"+format.format(date));

The parameters of GregorianCalendar are: year, month -1, day, hour, minute, second.

or


java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
java.util.Date date= format.parse("2007-12-25"); 
System.out.println("2007 Christmas is:"+format.format(date));

3. Take each part of the date


int year =calendar.get(Calendar.YEAR); 
int month=calendar.get(Calendar.MONTH)+1; 
int day =calendar.get(Calendar.DAY_OF_MONTH); 
int hour =calendar.get(Calendar.HOUR_OF_DAY); 
int minute =calendar.get(Calendar.MINUTE); 
int second =calendar.get(Calendar.SECOND); 

You add 1 to the month

Get the maximum number of days in the current month


Calendar cal = Calendar.getInstance(); 
int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
System.out.println(day); 

5. Take the last day of the month


Calendar cal = Calendar.getInstance(); 
int maxDay=cals.getActualMaximum(Calendar.DAY_OF_MONTH); 
java.text.Format formatter3=new java.text.SimpleDateFormat("yyyy-MM-"+maxDay); 
System.out.println(formatter3.format(cal.getTime()));

6. Take the first day of the month


java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-01"); 
java.util.Date firstDay=new java.util.Date(); 
System.out.println("the month first day is "+formats.format(firstDay)); 

Figure out the number of days between the dates


java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
java.util.Date beginDate= format.parse("2007-12-24"); 
java.util.Date endDate= format.parse("2007-12-25"); 
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000); 
System.out.println(" Days apart ="+day); 

8. Date one year ago


java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd"); 
java.util.Date todayDate=new java.util.Date(); 
long beforeTime=(todayDate.getTime()/1000)-60*60*24*365; 
todayDate.setTime(beforeTime*1000); 
String beforeDate=formatter.format(todayDate); 
System.out.println(beforeDate); 

9. The date after 1 year


java.util.Date today=new java.util.Date(); 
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Today is "+dateFormat.format(today));
System.out.println("Now is "+dateTimeFormat.format(today));
0

10. Time after 10 hours


java.util.Date today=new java.util.Date(); 
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Today is "+dateFormat.format(today));
System.out.println("Now is "+dateTimeFormat.format(today));
1

101. 10 hours ago


java.util.Date today=new java.util.Date(); 
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Today is "+dateFormat.format(today));
System.out.println("Now is "+dateTimeFormat.format(today));
2

102. Monday and Sunday of the current date


SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
GregorianCalendar cal = new GregorianCalendar();


int dayInWeek = cal.get(Calendar.DAY_OF_WEEK);
int offset = 0;
if (dayInWeek == 1) {
 //  Sunday 
 offset = 6;
} else {
 //  week 1 To a week 6
 offset = dayInWeek - 2;
}
cal.add(GregorianCalendar.DAY_OF_MONTH, -offset);
String sday = dateFormat.format(cal.getTime());
cal.add(GregorianCalendar.DAY_OF_MONTH, 6);
String eday = dateFormat.format(cal.getTime());

System.out.println(" This week of the week 1:" + sday);
System.out.println(" It's Sunday this week :" + eday);

102. The week in which the current date is obtained belongs to the week of the year


java.util.Date today=new java.util.Date(); 
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Today is "+dateFormat.format(today));
System.out.println("Now is "+dateTimeFormat.format(today));
4

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: