Calendar conventional method in Java classes (calculated based on the time)

example:

/ ****
* Incoming specific date, return date specific additional month.
* @Param date date (2017-04-13)
* @return 2017-05-13
* @throws a ParseException
* /
Private subMonth String (String DATE) {throws a ParseException
the SimpleDateFormat the SimpleDateFormat SDF new new = ( "the MM-dd-YYYY") ;
a Date sdf.parse dt = (DATE);
Calendar Calendar.getInstance RightNow = ();
rightNow.setTime (dt);
rightNow.add (the Calendar.MONTH,. 1);
a Date rightNow.getTime DT1 = ();
String = RESTR sdf.format (DT1);
return RESTR;

 

Acquisition time
// using the default time zone and locale to get a calendar
Calendar cal = Calendar.getInstance ();
// When the assignment date when minutes and seconds usual six, remember to month index starts at 0, so take month to + 1
System.out.println ( "Year:" + cal.get (Calendar.YEAR));
System.out.println ( "month:" + (cal.get (of the Calendar.MONTH) + 1));
System.out .println ( "day:" + cal.get (Calendar.DAY_OF_MONTH));
System.out.println ( "time:" + cal.get (Calendar.HOUR_OF_DAY));
System.out.println ( "points:" + cal.get (Calendar.MINUTE));
System.out.println ( "seconds:" + cal.get (Calendar.SECOND)) ;

today is February 12, 2018, operating results:

Year: 2018
dated: 2
Day: 12
time: 15
minutes: 57
Seconds: 39

to set the time
January index from 0, also need to pay attention to when setting up, for example, we set the last second February 15 New Year's Eve countdown in : 2018-02-15 23:59:59
be like this:

CAL = Calendar.getInstance Calendar ();
// If you want to set a date can be set once the year, month, day, hour, since the beginning of the month index from 0 month assignment to -1
// cal.set (year, month The, DATE, hourOfDay, minute, SECOND);
cal.set (2018,. 1, 15, 23 is, 59, 59);

or may be set all at a single field:

// or 6 fields are set, since the month index assigned from 0 to month -1
cal.set (Calendar.YEAR, 2018);
cal.set (the Calendar.MONTH, Calendar.FEBRUARY);
cal.set ( Calendar.DAY_OF_MONTH, 15);
cal.set (Calendar.HOUR_OF_DAY, 23 is);
cal.set (Calendar.MINUTE, 59);
cal.set (Calendar.SECOND, 59);
System.out.println (cal.getTime ( ));

time to print the results as follows:

Thu Feb 15 23:59:59 CST 2018

time calculating
add method:
such as the New Year's Eve last second, one second add:

CAL = Calendar.getInstance Calendar ();
System.out.println (cal.getTime ());
cal.set (2018,. 1, 15, 23 is, 59, 59);
cal.add (Calendar.SECOND,. 1);
System.out.println (cal.getTime ());

print time as a result, will automatically enter the date to the next day:

Feb 15 23:59:59 CST 2018 Thu
Fri Feb 16 00:00:00 CST 2018

Another example is the number of January 31, when the month plus one, there will be what are the results:

Calendar cal = Calendar.getInstance();
cal.set(2018, 1, 31, 8, 0, 0);
System.out.println(cal.getTime());
cal.add(Calendar.MONTH, 1);
System.out.println(cal.getTime());

运行结果:

Wed Jan 31 08:00:00 CST 2018
Wed Feb 28 08:00:00 CST 2018

Guess you like

Origin www.cnblogs.com/mark5/p/11320348.html