time operations for the Java Calendar classtags: Javacalendar Time Date2013-07-30 17:53 140401 People read comments (7) favorite reports Classification:all
(165) Java algorithm
(in)
Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.
Java Calendar class Time operations, which is perhaps the simplest to create a calendar and management, the demonstration 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
Calendar month starts at 0, which means 12 months a year from 0 to 11.
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 Plain copy
- 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 Plain copy
- A microsecond time Difference.
- Long val = Calendarend.gettimeinmillis ()-calendarbegin.gettimeinmillis ();
- Get the number of days after conversion
Time operations for the Java Calendar class