Java Operations on time

Source: Internet
Author: User
Tags dateformat

1.Date Get current time

1.1 Convert time milliseconds to date format.

Importnew Date (System.currenttimemillis ()); // The current millisecond time is passed String time=d.tolocalestring (); // returns the 2015-8-17 11:08:26 format string // format required by using SimpleDateFormat conversion Import Java.text.SimpleDateFormat; SimpleDateFormat sdf=new simpledateformat ("Yyyy-mm-dd hh:mm:ss"//  Output 2015-8-17 11:08:26

1.2 Get Time Example

 Public voidgettimebydate () {Date Date=NewDate ();D Ateformat df1= Dateformat.getdateinstance ();//date format, accurate to daySystem.out.println (Df1.format (date));D Ateformat df2= Dateformat.getdatetimeinstance ();//can be accurate to time division secondsSystem.out.println (Df2.format (date));D Ateformat df3= Dateformat.gettimeinstance ();//only shows the secondsSystem.out.println (Df3.format (date));D Ateformat df4= Dateformat.getdatetimeinstance (dateformat.full,dateformat.full);//display date, week, afternoon, time (accurate to seconds)System.out.println (Df4.format (date)); DateFormat Df5= Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long);//display date, last afternoon, time (accurate to seconds)System.out.println (Df5.format (date));D Ateformat Df6= Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short);//display date, last afternoon, time (accurate to minute)System.out.println (Df6.format (date));D Ateformat df7= Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium);//Display date, time (accurate to minute)System.out.println (Df7.format (date));  Public voidGettimebycalendar () {Calendar cal=calendar.getinstance ()intYear = Cal.get (calendar.year);//Get yearintMonth=cal.get (Calendar.month);//Get MonthintDay=cal.get (calendar.date);//Get DayintHour=cal.get (Calendar.hour);//hoursintMinute=cal.get (Calendar.minute);//pointsintSecond=cal.get (Calendar.second);//secondsintWeekOfYear = Cal.get (Calendar.day_of_week);//the day of the weekSystem.out.println ("The current time is: A.D." +year+ "Year" +month+ "month" +day+ "Day" +hour+ "when" +minute+ "Minute" +second+ "second week" +weekofyear); }

Cond...

Java Operations on time

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.