Use of old Java dates

Source: Internet
Author: User

// System.currenttimemillis    Private Static void test1 () {        // gets a millisecond value that returns the current time in milliseconds from January 1, 1970 0:0 0 seconds        long haomiao=  System.currenttimemillis ();        System.out.println (Haomiao); // 1509780008925    }
System.currenttimemillis
//java.util.Date not easy to internationalize    Private Static voidtest2 () {//Current date and time (first construct, no parameter)Date d1=NewDate (); System.out.println (d1);//Sat Nov 15:36:06 CSTSystem.out.println (D1.tostring ());//Sat Nov 15:36:06 CST//gets the millisecond value of the current timeSystem.out.println (D1.gettime ());//1509780539275//Current date and time (second construct, parameter is timestamp (milliseconds))Date d2=NewDate (1509780539275L);        System.out.println (D2); //Set time (modification time)Date d3=NewDate (); D3.settime (591423630000L); SYSTEM.OUT.PRINTLN (D3);//Wed Sep 12:20:30 CST 1988//Date Comparison        BooleanBafter=d2.after (D3);//true        BooleanBbefore=d2.before (D3);//false        BooleanBequals=d2.equals (D3);//false        intNum=d2.compareto (D3);//1//Create a Java.sql.Date instanceJava.sql.Date sqldate=NewJava.sql.Date (1509780539275L); System.out.println (sqldate);//2017-11-04}
java.util.Date not easy to internationalize
//Java.text.SimpleDateFormat is easy to internationalize    Private Static voidtest3 () {//format a date in a current locale using a static factoryString strdate= dateformat.getdateinstance (). Format (NewDate ()); System.out.println (strdate);//2017-11-4String Strdate_full= Dateformat.getdateinstance (dateformat.full). Format (NewDate ()); System.out.println (strdate_full);//Saturday, November 4, 2017String Strdate_long= Dateformat.getdateinstance (dateformat.long). Format (NewDate ()); System.out.println (Strdate_long);//November 4, 2017//use a static factory to format a date and time in a current language environmentString strdatetime= dateformat.getdatetimeinstance (). Format (NewDate ()); System.out.println (strdatetime);//2017-11-4 16:57:07String Strdatetime_full= Dateformat.getdatetimeinstance (dateformat.full,dateformat.full). Format (NewDate ()); System.out.println (strdatetime_full);//November 4, 2017 Saturday 05:04 P.M. 26 sec CST//square Date with default formattingSimpleDateFormat sdf=NewSimpleDateFormat (); String nowdate= Sdf.format (NewDate ()); System.out.println (nowdate);//17-11-4 pm 3:40//format date in the specified formatSimpleDateFormat sdf_my=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String mydate= Sdf_my.format (NewDate ()); System.out.println (mydate);//2017-11-04 15:43:11//parses a string into a date in the specified format        Try{Date formatedate= Sdf_my.parse ("2017-01-04 15:43:11"); System.out.println (Formatedate.tostring ());//Wed Jan 15:43:11 CST}Catch(ParseException e) {e.printstacktrace (); }    }
Java.text.SimpleDateFormat is easy to internationalize
 //Java.util.Calendar    Private Static voidTest4 ()throwsParseException {//generate a Calendar instanceCalendar calendar=calendar.getinstance (); intYear=Calendar.get (calendar.year); intMonth=Calendar.get (Calendar.month); intday=Calendar.get (calendar.date); intHour=Calendar.get (Calendar.hour); intmin=Calendar.get (Calendar.minute); intSec=Calendar.get (Calendar.second); intMsec=Calendar.get (Calendar.millisecond); intweek=Calendar.get (Calendar.day_of_week); System.out.println ("Year:" +year);//Year:System.out.println ("Month:" +month);//month: October less than one monthsSystem.out.println ("Day:" +day);//Day: 4System.out.println ("Time:" +hour);//Time: 4 It's 4 o'clock in the afternoon .System.out.println ("Points:" +min);//Score: TenSystem.out.println ("Seconds:" +sec);//seconds:System.out.println ("MS:" +msec);//milliseconds: TenSystem.out.println ("Week:" +week);//Week: 7 weeks from Sunday to the first day//get a date instance from a calendar instanceDate nowdate=Calendar.gettime (); System.out.println (Nowdate.tostring ());//Sat Nov 16:10:27 CST//Get millisecond value        Longhaomiao=Calendar.gettimeinmillis (); System.out.println (Haomiao);//1509783027010//formats the time in the specified formatSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String strnowdate=Sdf.format (nowdate); System.out.println (strnowdate);//2017-11-04 16:10:27//added 2 daysCalendar.add (calendar.day_of_month,2); System.out.println (Sdf.format (Calendar.gettime ()));//2017-11-06 16:16:12//reduced FebruaryCalendar.add (calendar.month,-2); System.out.println (Sdf.format (Calendar.gettime ()));//2017-09-06 16:18:30//set year (Year of revision)Calendar.set (calendar.year,2010); System.out.println (Sdf.format (Calendar.gettime ()));//2010-09-06 16:20:25//set time with dateCalendar.settime (Sdf.parse ("2012-01-02 11:22:33")); System.out.println (Sdf.format (Calendar.gettime ()));//2012-01-02 11:22:33//set the time with a millisecond valueCalendar.settimeinmillis (867730830000L); System.out.println (Sdf.format (Calendar.gettime ()));//1997-07-01 12:20:30}
Java.util.Calendar

Use of old Java dates

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.