Time operations for the Java Calendar class

Source: Internet
Author: User
Tags dateformat format definition iso 8601

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
  1. Package demo;
  2. Import java.util.Date;
  3. Import java.text.SimpleDateFormat;
  4. Import java.text.DateFormat;
  5. Import java.text.ParseException;
  6. Import java.util.Calendar;
  7. Public class Test
  8. {
  9. public Test ()
  10. {
  11. }
  12. public static void main (string[] Args)
  13. {
  14. //string Conversion Date format
  15. //dateformat fmtdatetime = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss");
  16. //receive Incoming Parameters
  17. //String strdate = args[1];
  18. //get date Format Object
  19. //date date = Fmtdatetime.parse (strdate);
  20. //full Display of Today's date and time
  21. String str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SSS")). format (new Date ());
  22. System.out.println (str);
  23. //create Calendar Object
  24. Calendar Calendar = Calendar.getinstance ();
  25. Try
  26. {
  27. //how to set the time for calendar
  28. //set the incoming time format
  29. SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy-m-d h:m:s");
  30. //specify a Date
  31. Date date = Dateformat.parse ("2013-6-1 13:24:16");
  32. Date set to date for calendar
  33. Calendar.settime (date);
  34. //display the time just set in a specific format
  35. str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SSS")). format (calendar.gettime ());
  36. System.out.println (str);
  37. }
  38. Catch (parseexception e)
  39. {
  40. E.printstacktrace ();
  41. }
  42. //or Another setup calendar way
  43. //divided for year, month, date, hourofday, minute, Second
  44. Calendar = Calendar.getinstance ();
  45. Calendar.set (1, 2, n, 44);
  46. str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SSS")). format (calendar.gettime ());
  47. System.out.println (str);
  48. //Calendar How to get the current time
  49. //initialize (reset) Calendar Object
  50. Calendar = Calendar.getinstance ();
  51. //or Initialize Calendar object with Date
  52. Calendar.settime (new Date ());
  53. //settime similar to the above line
  54. //date date = new Date ();
  55. //calendar.settime (date);
  56. str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SSS")). format (calendar.gettime ());
  57. System.out.println (str);
  58. //display Year
  59. int year = Calendar.get (calendar.year);
  60. System.out.println ("year was =" + string.valueof (year));
  61. //display month (starting from 0, actual display to add One)
  62. int month = Calendar.get (calendar.month);
  63. System.out.println ("nth is =" + (month + 1));
  64. //week
  65. int week = Calendar.get (calendar.day_of_week);
  66. System.out.println ("week is =" + week);
  67. //nth Day of the year
  68. int day_of_year = Calendar.get (calendar.day_of_year);
  69. System.out.println ("day_of_year is =" + day_of_year);
  70. //N days of the Month
  71. int day_of_month = Calendar.get (calendar.day_of_month);
  72. System.out.println ("day_of_month =" + string.valueof (day_of_month));
  73. //3 hours later
  74. Calendar.add (calendar.hour_of_day, 3);
  75. int hour_of_day = Calendar.get (calendar.hour_of_day);
  76. System.out.println ("hour_of_day + 3 =" + hour_of_day);
  77. //current number of minutes
  78. int MINUTE = Calendar.get (calendar.minute);
  79. System.out.println ("MINUTE =" + MINUTE);
  80. //15 minutes later
  81. Calendar.add (calendar.minute, 15);
  82. MINUTE = Calendar.get (calendar.minute);
  83. System.out.println ("MINUTE + =" + MINUTE);
  84. //30 minutes ago
  85. Calendar.add (calendar.minute,-30);
  86. MINUTE = Calendar.get (calendar.minute);
  87. System.out.println ("MINUTE-30 =" + MINUTE);
  88. //formatted Display
  89. str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SS")). format (calendar.gettime ());
  90. System.out.println (str);
  91. //reset Calendar Show Current Time
  92. Calendar.settime (new Date ());
  93. str = (new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss:SS")). format (calendar.gettime ());
  94. System.out.println (str);
  95. //create a Calendar to compare time
  96. Calendar calendarnew = calendar.getinstance ();
  97. //set to 5 hours ago, latter large, display-1
  98. Calendarnew.add (calendar.hour,-5);
  99. System.out.println ("time comparison:" + calendarnew.compareto (calendar));
  100. //set 7 hours later, The former large, showing 1
  101. Calendarnew.add (calendar.hour, +7);
  102. System.out.println ("time comparison:" + calendarnew.compareto (calendar));
  103. //return 2 hours, same time, display 0
  104. Calendarnew.add (calendar.hour,-2);
  105. System.out.println ("time comparison:" + calendarnew.compareto (calendar));
  106. }
  107. }


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
      1. A microsecond time Difference.
      2. Long val = Calendarend.gettimeinmillis ()-calendarbegin.gettimeinmillis ();
      3. Get the number of days after conversion

Time operations for the Java Calendar class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.