Java-Timestamp, date, Calendar, SimpleDateFormat

Source: Internet
Author: User

Java
Package Com.ikoo.demo; Import Java.text.simpledateformat;import Java.util.calendar;import java.util.Date; public class Dateutil {public static void main (string[] args) {/** * timestamp * 1.system.currenttim         Emillis () * 2.new Date (). GetTime () * 3.calendar.getinstance (). Gettimeinmillis () * p.s. * Java acquisition Timestamp is accurate to the millisecond level, UNIX is accurate to the second level, so Java gets the timestamp needs to divide by 1000, can guarantee consistency * calendar.getinstance (). Gettimeinmillis () is the most time-consuming because C        Anlendar to handle time zone problems */SYSTEM.OUT.PRINTLN ("System.currenttimemillis ():" + System.currenttimemillis ());        System.out.println ("New Date (). GetTime ():" + new Date (). GetTime ());        System.out.println ("Calendar.getinstance (). Gettimeinmillis ():" + calendar.getinstance (). Gettimeinmillis ());         System.out.println ("----------");        /** * Timestamp (date) Convert custom date format */Long TimeStamp = System.currenttimemillis ();        System.out.println ("System.currenttimemillis ():" + TimeStamp); DatE date = new Date ();        System.out.println ("New Date ():" + date);        Long Datetimastamp = Date.gettime ();        System.out.println ("Date.gettime ():" + date.gettime ());        String formatDate0 = Dateutil.timestamptoformatdate (System.currenttimemillis (), "Yyyy-mm-dd HH:mm:ss");        System.out.println ("FormatDate0:" + formatDate0);        String formatDate1 = dateutil.timestamptoformatdate (New Date (), "Yyyy-mm-dd HH:mm:ss");        System.out.println ("formatDate1:" + formatDate1);        String FormatDate2 = dateutil.timestamptoformatdate (new Date (). GetTime (), "Yyyy-mm-dd HH:mm:ss");        System.out.println ("FormatDate2:" + formatDate2);        String formatDate3 = Dateutil.timestamptoformatdate (Calendar.getinstance (). GetTime (), "Yyyy-mm-dd HH:mm:ss");    System.out.println ("FormatDate3:" + formatDate3);                }/** * Timestamp converted to formatted date * * @param dateobj Time Object * System.currenttimemillis () *             New Date () *   New Date (). GetTime () * calendar.getinstance (). Gettimeinmillis () * @param pattern Date format * YYYY-MM-DD HH:MM:SS * YYYY/MM/DD HH:mm:ss * yyyy year MM month DD Day HH:MM:SS * @r        Eturn Sdf.format (Object dateobj) */public static string Timestamptoformatdate (object Dateobj, string pattern) {        SimpleDateFormat SDF = new SimpleDateFormat (pattern);        String formatdate = Sdf.format (dateobj);    return formatdate; }}


Console Print Results:

System.Currenttimemillis():1503660334272NewDate ().GetTime():1503660334274Calendar.getinstance().Gettimeinmillis():1503660334299----------System.Currenttimemillis():1503660334328NewDate (): Fri -  +: -: theCst .Date.GetTime():1503660334328FORMATDATE0:2017-08-25  +: -: theFormatDate1:2017-08-25  +: -: theFormatDate2:2017-08-25  +: -: theFormatDate3:2017-08-25  +: -: theProcess finished with exit code0

Java-Timestamp, date, Calendar, SimpleDateFormat

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.