Time operations for Java calendar classes __java

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

The time operation of the Java Calendar class, which may be the simplest scenario for creating and managing calendars and operating times in a Java environment, and the demo code is simple.

Shows the time of acquisition, the cumulative and exhausting of datetime, and the comparison of date and time.

Original address: blog.csdn.net/joyous/article/details/9630893

Precautions:

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 values are as follows, starting every week from SUNDAY, from 1 ~ 7:

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 [context sensitive] Month July; June June; 07
L Month in year (standalone form) Month July; June June; 07
W Week in the Year Number 27
W Week in month Number 2
D Day in year Number 189
D Day in month Number 10
F Day of 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 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//Dateform
    At 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 the time for the Calendar method//sets the time format passed in 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 has just been 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)); }
}

Run results

2013-06-07 23:27:34:195
2013-06-01 13:24:16:000
2013-02-02
17:35:44:197 2013-06-07 23:27:34:197 Year is = 2013
nth are = 6
Week is = 6
day_of_year is = 158
Day_of_month = 7
hour_of_day + 3 = 2
MI Nute =
MINUTE + =
MINUTE-30 =
2013-06-08 02:12:34:197 2013-06-07
Time comparison: -1
Time Comparison: 1
time comparison: 0

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);


Original address: blog.csdn.net/joyous/article/details/9630893

http://blog.csdn.net/joyous/article/details/9630893

Q Group discussion: 236201801


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.