Processing of date and time in Java

Source: Internet
Author: User

1. You can obtain the system time through the default constructor or instance of Calendar or Date. However, the current Date is mainly processed by the Calendar ar. In addition, we can also obtain the day of the week (starting from Sunday), day [java] public static String getCurrentTime () {Calendar c = Calendar. getInstance (); // HH 24 hour & hh 12 hour, E-week SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy e hh: mm: ss "); return sdf. format (c. getTime ();} public static void printTodayInfo () {Date date = new Date (); Calendar c = Calendar ar. getInstance (); c. setTime (date); System. out. println ("Time For today: "+ date. toString (); System. out. println ("Day for today:" + c. get (Calendar. DAY_OF_WEEK); System. out. println ("Time for today:" + c. get (Calendar. DAY_OF_MONTH); System. out. println ("Time for today:" + c. get (Calendar. DAY_OF_YEAR);} 2. obtain the historical time compared to the current time, such as yesterday, last week, last month, and last year. [java] public static String getYesterday () {Calendar c = Calendar. getInstance (); c. add (Calendar. DATE,-1); SimpleDateFormat sdf = new S ImpleDateFormat ("MM/dd/yyyy"); return sdf. format (c. getTime ();} public static String getLastMonth () {Calendar c = Calendar. getInstance (); c. add (Calendar. MONTH,-1); SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy"); return sdf. format (c. getTime ();} public static String getLastYear () {Calendar c = Calendar. getInstance (); c. add (Calendar. YEAR,-1); SimpleDateFormat sdf = new SimpleDateForma T ("MM/dd/yyyy"); return sdf. format (c. getTime ();} 3. set a historical time [java] public static void setDate () {Calendar c = Calendar. getInstance (); // note that the month start from 0 c. set (2013, 0, 25, 14, 48, 20); SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy HH: mm: ss"); System. out. println (sdf. format (c. getTime (); String birthdate = "01/25/2012 13:11:12"; Date date = new Date (); try {date = sdf. p Arse (birthdate);} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();} c. setTime (date); System. out. println (sdf. format (c. getTime ();} 4. the comparison between the time and the long type shows that the time corresponding to 0 is 08:00:00, and 1000 will be added for every 1 s increase in the future. In this way, we can get different time. [Java] public static void checkLongAndDate () {Calendar c = Calendar. getInstance (); c. setTimeInMillis (0); SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy HH: mm: ss"); // output is 01/01/1970 08:00:00 System. out. println (sdf. format (c. getTime (); c. setTimeInMillis (c. getTimeInMillis () + 1000*60*60*24); System. out. println (sd

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.