Summary of Date Processing in java

Source: Internet
Author: User

Summary of Date Processing in java

This article summarizes the date processing method before jdk8. the new date Processing Method in jdk8.

Package com. test; import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; public class DateAndTime {public static void main (String [] args) {int year; int month; int day; // old version Date date = new Date (); year = date. getYear () + 1900; month = date. getMonth () + 1; day = date. getDate (); System. out. println ("old --" + year + "year" + month + "month" + day + "day"); // the new version of Calendar calendar = Calendar. getInstance (); year = calendar. get (Calendar. YEAR); month = calendar. get (Calendar. MONTH) + 1; day = calendar. get (Calendar. DATE); System. out. println ("New --" + year + "year" + month + "month" + day + "day"); // The number of milliseconds long now = System. currentTimeMillis (); Date date2 = new Date (now); // or: date. setTime (now); long --> Date System. out. println (date2.getDate (); // time type conversion long <-> Date <-> Calendar calendar. setTime (date2); // Date --> Calendar System. out. println (calendar. get (Calendar. YEAR); calendar. getTime (); // Calendar --> Date System. out. println (calendar. getTime (). getDate (); date. getTime (); System. out. println (date. getTime (); // Date --> long // time input and output SimpleDateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); string today = fmt. format (date2); // Date --> String try {Date date3 = fmt. parse ("16:20:00"); // String --> Date System. out. println (date3);} catch (ParseException e) {e. printStackTrace ();} System. out. println (today); // use the Calendar time to calculate the calendar. add (Calendar. YEAR, 2); calendar. add (Calendar. MONTH, 1); System. out. println (fmt. format (calendar. getTime (); // Today is July 22, December 19, 2015. This should be July 15, January 19, 2018 }}

The console is printed as follows:

Old -- New in December 20152015 -- 192015191450513438352Sat Dec 19 16:20:00 CST 582018-12-19 16: 23:-01-19 16:23:58

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.