Java calendar class time operations

Source: Internet
Author: User

Java calendar time operations, the code is very simple.

This example shows how to get the time, how to accumulate and subtract the date and time.

 

Package mytest;/** demonstrate the general operation of calendar */import Java. text. simpledateformat; import Java. util. date; import Java. util. calendar; public class test {public test () {} public static void main (string [] ARGs) {// complete display Date and Time string STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS: SSS ")). format (new date (); system. out. println (STR); // create the Calendar Object calendar = calendar. getinstance (); // initialize the Calendar Object, but it is not necessary unless you need to reset the time calendar. settime (new date (); // settime is similar to the previous line // Date = new date (); // calendar. settime (date); // display the year int year = calendar. get (calendar. year); system. out. println ("year is =" + String. valueof (year); // display the month (starting from 0, actually show to add one) int month = calendar ar. get (calendar. month); system. out. println ("month is =" + (month + 1); // the 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); // the nth day of the month int day_of_month = calendar. get (calendar. day_of_month); system. out. println ("day_of_month =" + String. valueof (day_of_month); // calendar after 3 hours. 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 ar. get (calendar. minute); system. out. println ("minute =" + minute); // calendar in 15 minutes. add (calendar. minute, 15); minute = calendar. get (calendar. minute); system. out. println ("minute + 15 =" + minute); // calendar 30 minutes ago. add (calendar. minute,-30); minute = calendar. get (calendar. minute); system. out. println ("Minute-30 =" + minute); // formatted STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS ")). format (calendar. gettime (); system. out. println (STR); // reset the calendar to display the current time in the calendar. settime (new date (); STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS ")). format (calendar. gettime (); system. out. println (STR); // create a calendar to compare the time of calendar. getinstance (); // set to 5 hours ago, the latter is large, and-1 calendarnew is displayed. add (calendar. hour,-5); system. out. println ("time comparison:" + calendarnew. compareto (calendar); // After the value is set to 7 hours, the former is large and 1 calendarnew is displayed. add (calendar. hour, + 7); system. out. println ("time comparison:" + calendarnew. compareto (calendar); // returns two hours. The time is the same. 0 calendarnew is displayed. add (calendar. hour,-2); system. out. println ("time comparison:" + calendarnew. compareto (calendar ));}}

 

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.