Java time calculation favorites

Source: Internet
Author: User

Java code
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Public class DateAll {
Public static void main (String [] args) throws Exception {
// Current time
Calendar cal = Calendar. getInstance ();
// SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss g e d f w W a e f ");
String mDateTime = formatter. format (cal. getTime ());
System. out. println (mDateTime );
// Date 1 year ago
Java. util. Date myDate = new java. util. Date ();
Long myTime = (myDate. getTime ()/1000)-60*60*24*365;
MyDate. setTime (myTime * 1000 );
String mDate = formatter. format (myDate );
System. out. println (mDate );
// Tomorrow's date
MyDate = new java. util. Date ();
MyTime = (myDate. getTime ()/1000) + 60*60*24;
MyDate. setTime (myTime * 1000 );
MDate = formatter. format (myDate );
System. out. println (mDate );
// Number of days between two times
SimpleDateFormat myFormatter = new SimpleDateFormat ("yyyy-MM-dd ");
Java. util. Date date = myFormatter. parse ("1883-10-10 ");
Java. util. Date mydate = myFormatter. parse ("1899-12-30 ");
Long day = (date. getTime ()-mydate. getTime ()/(24*60*60*1000 );
System. out. println (day );
// Add half an hour
SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Java. util. Date date1 = format. parse ("23:16:00 ");
Long Time = (date1.getTime ()/1000) + 60*30;
Date1.setTime (Time * 1000 );
String mydate1 = formatter. format (date1 );
System. out. println (mydate1 );
// Calculate the date of year, month, and week
SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy-mm f e ");
Java. util. Date date2 = formatter2.parse ("Friday 2003-05 ");
SimpleDateFormat formatter3 = new SimpleDateFormat ("yyyy-MM-dd ");
String mydate2 = formatter3.format (date2 );
System. out. println (mydate2 );
// The day of the week
Mydate = myFormatter. parse ("2007-11-9 ");
SimpleDateFormat formatter4 = new SimpleDateFormat ("E ");
String mydate3 = formatter4.format (mydate );
System. out. println (mydate3 );
}
}

Author: "sailei1"
 

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.