Java Calendar class time operations, which is perhaps the simplest scenario for creating and managing calendars, the demo code is simple.
Demonstrates the acquisition time, the accumulation and subtraction of date time, and the comparison.
Original address: blog.csdn.net/joyous/article/details/9630893
Precautions:
Calendar month starts at 0, which means 12 months a year from 0 to 11.
The Calendar.day_of_week definition and values are as follows:
Calendar.sunday = 1
Calendar.monday = 2
Calendar.tuesday = 3
Calendar.wednesday = 4
Calendar.thursday = 5
Calendar.friday = 6
Calendar.saturday = 7
Format definition for SimpleDateFormat
| Letter
Date or Time Component |
Presentation |
Examples |
G |
Era designator |
Text |
AD |
y |
Year |
Year |
1996 ;96 |
Y |
Week year |
Year |
2009 ;09 |
M |
Month in year (context sensitive) |
Month |
July ; Jul ;07 |
L |
Month in year (standalone form) |
Month |
July ; Jul ;07 |
w |
Week in year |
Number |
27 |
W |
Week in month |
Number |
2 |
D |
Day in year |
Number |
189 |
d |
Day in month |
Number |
10 |
F |
Day's week in month |
Number |
2 |
E |
Day name in week |
Text |
Tuesday ;Tue |
u |
Day number of week (1 = Monday, ..., 7 = Sunday) |
Number |
1 |
a |
AM/PM Marker |
Text |
PM |
H |
Hour in Day (0-23) |
Number |
0 |
k |
Hour in Day (1-24) |
Number |
24 |
K |
Hour in AM/PM (0-11) |
Number |
0 |
h |
Hour in AM/PM (1-12) |
Number |
12 |
m |
Minute in Hour |
Number |
30 |
s |
Second in minute |
Number |
55 |
S |
Millisecond |
Number |
978 |
z |
Time zone |
General Time Zone |
Pacific Standard Time ; PST ;GMT-08:00 |
Z |
Time zone |
RFC 822 Time Zone |
-0800 |
X |
Time zone |
ISO 8601 time Zone |
-08 ; -0800 ;-08:00 |
The Java Calendar demo code looks like this:
[Java]View PlainCopy
- Package demo;
- Import Java.util.Date;
- Import Java.text.SimpleDateFormat;
- Import Java.text.DateFormat;
- Import java.text.ParseException;
- Import Java.util.Calendar;
- Public class Test
- {
- Public Test ()
- {
- }
- public static void Main (string[] args)
- {
- //String conversion date format
- //DateFormat fmtdatetime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- //Receive incoming parameters
- //String strdate = args[1];
- //Get date Format Object
- //Date date = Fmtdatetime.parse (strdate);
- //Full display of today's date and time
- String str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (new Date ());
- System.out.println (str);
- //Create Calendar Object
- Calendar calendar = Calendar.getinstance ();
- Try
- {
- //How to set the time for calendar
- //Set the incoming time format
- SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy-m-d h:m:s");
- //Specify a date
- Date date = Dateformat.parse ("2013-6-1 13:24:16");
- date set to date for calendar
- Calendar.settime (date);
- //Display the time just set in a specific format
- str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (Calendar.gettime ());
- System.out.println (str);
- }
- catch (parseexception e)
- {
- E.printstacktrace ();
- }
- //or another setup calendar way
- //Divided for year, month, date, hourofday, minute, second
- Calendar = Calendar.getinstance ();
- Calendar.set (1, 2, N, 44);
- str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (Calendar.gettime ());
- System.out.println (str);
- //Calendar How to get the current time
- //Initialize (reset) Calendar object
- Calendar = Calendar.getinstance ();
- //or Initialize Calendar object with Date
- Calendar.settime (new Date ());
- //settime similar to the above line
- //Date date = new Date ();
- //Calendar.settime (date);
- str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (Calendar.gettime ());
- System.out.println (str);
- //Display year
- int year = Calendar.get (calendar.year);
- SYSTEM.OUT.PRINTLN ("Year was =" + string.valueof (year));
- //Display month (starting from 0, actual display to add one)
- int month = Calendar.get (calendar.month);
- System.out.println ("Nth is =" + (month + 1));
- //week
- int week = Calendar.get (Calendar.day_of_week);
- System.out.println ("Week is =" + week);
- //Nth Day of the year
- int day_of_year = Calendar.get (calendar.day_of_year);
- System.out.println ("day_of_year is =" + day_of_year);
- //N days of the month
- int day_of_month = Calendar.get (calendar.day_of_month);
- System.out.println ("day_of_month =" + string.valueof (day_of_month));
- //3 hours later
- Calendar.add (Calendar.hour_of_day, 3);
- int hour_of_day = Calendar.get (Calendar.hour_of_day);
- System.out.println ("Hour_of_day + 3 =" + Hour_of_day);
- //Current number of minutes
- int MINUTE = Calendar.get (Calendar.minute);
- System.out.println ("MINUTE =" + MINUTE);
- //15 minutes later
- Calendar.add (Calendar.minute, 15);
- MINUTE = Calendar.get (Calendar.minute);
- System.out.println ("MINUTE + =" + MINUTE);
- //30 minutes ago
- Calendar.add (Calendar.minute,-30);
- MINUTE = Calendar.get (Calendar.minute);
- System.out.println ("MINUTE-30 =" + MINUTE);
- //Formatted display
- str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS")). Format (Calendar.gettime ());
- System.out.println (str);
- //Reset Calendar show Current time
- Calendar.settime (new Date ());
- str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS")). Format (Calendar.gettime ());
- System.out.println (str);
- //Create a Calendar to compare time
- Calendar calendarnew = Calendar.getinstance ();
- //set to 5 hours ago, latter large, display-1
- Calendarnew.add (Calendar.hour,-5);
- System.out.println ("Time comparison:" + Calendarnew.compareto (calendar));
- //Set 7 hours later, the former large, showing 1
- Calendarnew.add (Calendar.hour, +7);
- System.out.println ("Time comparison:" + Calendarnew.compareto (calendar));
- //Return 2 hours, same time, display 0
- Calendarnew.add (Calendar.hour,-2);
- System.out.println ("Time comparison:" + Calendarnew.compareto (calendar));
- }
- }
To calculate the time difference, you can use Calendar.gettimeinmillis () to obtain a microsecond of two times, and then convert it, for example, to obtain the difference in days, the code is as follows:
[Java]View PlainCopy
- A microsecond time difference.
- Long val = Calendarend.gettimeinmillis ()-Calendarbegin.gettimeinmillis ();
- Get the number of days after conversion
- Long day = val/(+ * 24);
Time operations for the Java Calendar class