Java Time Processing instance

Source: Internet
Author: User

1, in the use of SimpleDateFormat when the format of the time yyyy. MM.DD is a day of the year. If you want the format time to be 12-hour, use hh:mm:ss if you want the format time to be 24-hour, use HH:mm:ss

<span style= "White-space:pre" ></span>date d = new Date ();           SimpleDateFormat ss = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");//12-hour           System.out.println (Ss.format (d));            Date date = new Date ();           SimpleDateFormat Sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//24-hour           String lgtime = Sdformat.format ( Date);            

2. Time Calculation

DateFormat df=new SimpleDateFormat ("Yyyy-mm-dd EE hh:mm:ss");   System.out.println (Df.format (New Date ()));     Calendar C = calendar.getinstance ();     C.add (Calendar.month, 1); Current time plus 1 month   System.out.println (Df.format (C.gettime ()));     C.add (Calendar.hour, 3); Current time plus 3 hours   System.out.println (Df.format (C.gettime ()));     C.add (Calendar.year,-2); Current time minus 2 years   System.out.println (Df.format (C.gettime));     C.add (Calendar.day_of_week, 7); The current time adds 7 days   System.out.println (Df.format (C.gettime ()));   

3, take the first day of the month:

  1. Java.text.SimpleDateFormat format = new java.text.SimpleDateFormat ("yyyy-mm-01");
  2. Java.util.Date firstday=New java.util.Date ();
  3. System.out.println ("The monthfirst day is" +formats.format (FirstDay));

4, take the last day of the month:
  1. Calendar cal = Calendar.getinstance ();
  2. int maxday=cals.getactualmaximum (calendar.day_of_month);
  3. Java.text.Format formatter3=New Java.text.SimpleDateFormat ("yyyy-mm-"+maxday);
  4. System.out.println (Formatter3.format (Cal.gettime ()));


5. Find the number of days between two dates:

  1. Java.text.SimpleDateFormat format = new java.text.SimpleDateFormat ("YYYY-MM-DD");
  2. Java.util.Date begindate= format.parse ("2007-12-24");
  3. java.util.date enddate= format.parse" ( Span class= "string" style= "Color:blue" > "2007-12-25" );    
  4. long day= (Date.gettime ()-mydate.gettime ())/(* ** +);
  5. System.out.println ("number of days apart ="+day);

6, a year ago, the date:
  1. Java.text.Format formatter=New Java.text.SimpleDateFormat ("YYYY-MM-DD");
  2. java.util.date todaydate=new  java.util.date ();    
  3. long  beforetime= (Todaydate.gettime ()/ 1000 ) -60 *60 *24 * 365 ;    
  4. Todaydate.settime (beforetime*);
  5. String Beforedate=formatter.format (todaydate);
  6. System.out.println (beforedate);

7, one year after the date:
  1. Java.text.Format formatter=New Java.text.SimpleDateFormat ("YYYY-MM-DD");
  2. Java.util.Date todaydate=New java.util.Date ();
  3. long  aftertime= (Todaydate.gettime ()/ 1000 ) +60 *60 *24 * 365 ;    
  4. Todaydate.settime (aftertime*);
  5. String Afterdate=formatter.format (todaydate);
  6. System.out.println (afterdate);

8, 10 hours after the time
  1. Java.util.Calendar cal=java.util.calendar.getinstance ();
  2. Cal.settime (Dateoper);
  3. Cal.add (Java.util.Calendar.HOUR_OF_DAY,ten);
  4. System.out.println ("Date:"+forma.format (Cal.gettime ()));

9, 10 hours before the time
  1. Java.util.Calendar cal=java.util.calendar.getinstance ();
  2. Cal.settime (Dateoper);
  3. Cal.add (java.util.calendar.hour_of_day,-);
  4. System.out.println ("Date:"+forma.format (Cal.gettime ()));


Java Time Processing instance

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.