Time operations for Java calendar classes _java

Source: Internet
Author: User
Tags dateformat format definition set time

Java Calendar class time operations, which may be the simplest scenario for creating calendars and administration, the demo code is simple, demonstrating the time to get, the cumulative and exhausting of date times, and comparisons.

Note:

The month of the Calendar starts from 0, which is the 12 months of the year from 0 ~ 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

The Java Calendar Demo code is as follows:

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 parameter//String strdate = args[1]; 
 
 Gets the date format object//Dates date = Fmtdatetime.parse (strdate); 
 Full display today's date 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 {//Set time for the calendar to set the incoming time format simpledateformat DateFormat = new SimpleDateFormat ("yyyy-m-d h:m:s"); 
  Specify a date = Dateformat.parse ("2013-6-1 13:24:16"); 
 
  The date calendar.settime (date) to which calendar is set to date; Displays the time that you 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 set of calendar methods//To be divided into year, month, date, hourofday, minute, second calendar = Calendar.getinstance (); 
 Calendar.set (2013, 1, 2, 17, 35, 44); 
 str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (Calendar.gettime ()); 
 
 System.out.println (str); 
 Calendar to get the method of the current time//Initialize (reset) Calendar Object calendar = Calendar.getinstance (); 
 
 or use date to initialize the Calendar object Calendar.settime (new Date ()); 
 SetTime similar to the line above//Date date = new Date (); 
 
 Calendar.settime (date); 
 str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (Calendar.gettime ()); 
 
 System.out.println (str); 
 Show year int = Calendar.get (calendar.year); 
 
 System.out.println ("Year are =" + string.valueof (year)); 
 Displays the month (starting from 0, the actual display to add one) int month = Calendar.get (calendar.month); 
 
 System.out.println ("Nth is =" + (month + 1)); This week a few int week = Calendar.get (calenDar. 
 Day_of_week); 
 
 SYSTEM.OUT.PRINTLN ("Week is =" + week); 
 This year's nth day int day_of_year = Calendar.get (calendar.day_of_year); 
 
 System.out.println ("day_of_year is =" + day_of_year); 
 This month the nth day 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); 
 The 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); 
 Format shows str = (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS")). Format (Calendar.gettime ()); System.ouT.println (str); 
 Reset Calendar Displays the 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, the latter large, display-1 Calendarnew.add (Calendar.hour,-5); 
 
 System.out.println ("Time comparison:" + Calendarnew.compareto (calendar)); 
 Set 7 hours later, the former is large, showing 1 calendarnew.add (Calendar.hour, +7); 
 
 System.out.println ("Time comparison:" + Calendarnew.compareto (calendar)); 
 Return 2 hours, same time, show 0 calendarnew.add (Calendar.hour,-2); 
 System.out.println ("Time comparison:" + Calendarnew.compareto (calendar));  } 
}

To calculate the time difference, the Calendar.gettimeinmillis () can be used to obtain two times the microsecond interval, and then be converted, such as to obtain the number of days, the code is as follows:

Microsecond time difference 
long val = Calendarend.gettimeinmillis ()-Calendarbegin.gettimeinmillis (); 
The conversion gets the number of days 
long day = val/(1000 * 60 * 60 * 24);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.