Two examples of Java dates

Source: Internet
Author: User

Example 1
Find the date 10 days ago.

Package Date;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Calendar;import Java.util.date;import Java.util.scanner;public class Date01 {public static void main (string[] args) throws parseexception {Scanner SCA = new Scanner (system.in); System.out.println ("Please enter a date:"); String str = sca.nextline (); SimpleDateFormat s = new SimpleDateFormat ("YYY-MM-DD");D ate d = s.parse (str); The format conversion converts a string to Datecalendar C = calendar.getinstance (); The Calendar class is an abstract class that provides methods for manipulating calendar fields, such as getting dates after a few days. (C.add) C.settime (d); C.add (Calendar.day_of_month,-10); SimpleDateFormat time = new SimpleDateFormat ("Yyy-mm-dd"); System.out.println (Time.format (C.gettime ()))//string time = S.format (C.gettime ()); System.out.println ("The period 10 days ago is:" +time);}}


Example Two
Time difference problem
 Packagedate;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Scanner; Public classDATE02 { Public Static voidMain (string[] args)throwsparseexception {Scanner SCA=NewScanner (system.in); System.out.println ("Please enter a date:"); String Str=Sca.nextline (); SimpleDateFormat s=NewSimpleDateFormat ("Yyy-mm-dd HH:mm:ss"); Date D=s.parse (str); LongStart =D.gettime (); Start TimeLongEnd =System.currenttimemillis (); System TimeLongresult = end-start; LongTime = result/(1000*60); if(Time < 60) {System.out.println ( time+ "Minutes Ago"); }Else if(Time < 60*24) {System.out.println ( time/60+ "Hours Ago"); }Else if(Time < 60*24*30) {System.out.println ( time/(60*24) + "days Ago"); }Else if(Time > 60*24*30) {System.out.println (S.format (D.gettime ())); }    }}

  

Two examples of Java dates

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.