Java gets the time of various formats, gets yesterday's tomorrow date, gets the start end of the day

Source: Internet
Author: User
Tags date now dateformat
first, get the current date and time

1. Use Date and DateFormatDate now = new Date ();
DateFormat df1 = Dateformat.getdateinstance ();       Formatted time format: 2016-2-19 String str1 = D1.format (now); DateFormat D2 = Dateformat.getdatetimeinstance ();       Formatted time format: 2016-2-19 20:54:53 String str2 = D2.format (now); DateFormat d3 = Dateformat.gettimeinstance ();       Formatted time format: 20:54:53 String STR3 = D3.format (now); DateFormat D4 = Dateformat.getinstance (); Formatted time format: 16-2-29 afternoon 8:54 String STR4 = D4.format (now);
DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full,dateformat.full);       Formatted time format: Friday, February 19, 2016 08:54 P.M. 53 CST String STR5 = D5.format (now); DateFormat d6 = dateformat.getdatetimeinstance (Dateformat.long,dateformat.long);       Formatted time format: February 19, 2016 08:54 P.M. 53 second String STR6 = D6.format (now); DateFormat D7 = dateformat.getdatetimeinstance (Dateformat.short,dateformat.short);       Formatted time format: 16-2-19 afternoon 8:54 String STR7 = D7.format (now); DateFormat D8 = Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium); Formatted time format: 2016-2-19 20:54:53 String str8 = D8.format (now);2. Use Canlinder
Calendar C = calendar.getinstance (); int year = C.get (calendar.year);//Get years int month=c.get (calendar.month) +1;//get month int day=c.get (calendar.date); /Get Day int minute=c.get (calendar.minute),//int hour=c.get (Calendar.hour),//hour int second=c.get (Calendar. SECOND);//sec int weekofyear = C.get (Calendar.day_of_week);//Show the current date is the day ordinal of the week, Monday is 1, Friday is 5
String Date=year + "Year" + Month + "Month" + Day + "days"; Format: February 19, 2016 String Time=hour + "time" + Minute + "min" + second + "seconds"; Format: 8:54 53 sec String Date1=c.gettime ()//format: Fri Feb 20:54:53 CST 20163. Use date and SimpleDateFormat (advantages: 24 hours)Date date = new Date ();       SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Sdatesuffix = Dateformat.format (date); Summary: The most commonly used when the third method
Ii. access to yesterday's and tomorrow's datesDate Date=new date ()//fetch time Calendar calendar = new GregorianCalendar ();       Calendar.settime (date); Calendar.add (Calendar. DATE,-1)//reduce the date by one day, and if you want to push the date back one day, change the negative number to a positive date=calendar.gettime ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd"); String datestring = Formatter.format (date);get start time and end time of dayDate Date=new date ()//Time Taken
Date.cleartime ()
Calendar calendar = new GregorianCalendar ();
Calendar.settime (date);
Calendar.set (calendar.hour,0)
Calendar.set (calendar.minute,0)
Calendar.set (calendar.second,0)
Calendar.set (calendar.millisecond,0)
System.out.println ("Start Time:" +calendar.gettime ())
Calendar.set (calendar.hour,23)
Calendar.set (calendar.minute,59)
Calendar.set (calendar.second,59)
Calendar.set (calendar.millisecond,999)
System.out.println ("End Time:" +calendar.gettime ())

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.