Several common questions about dates

Source: Internet
Author: User
Problem


1. How do I calculate the interval between two times?

Interval =date1.gettime ()-date2.gettime (); The number of milliseconds.
Except for 1000 is a second, except 60 is a cent, and then 60 is an hour ..... .......... ...

Remember that all the time classes in the Java Standard library are converted on this basis, but he has written some
The method of transformation is only for you. But it all depends on the number of milliseconds.

2. T=calendar.getinstance (); M=t.get (T.month) +1; Why do you want to add one here?

In the Java language, date's month range is: 0~11, which differs 1 from the natural expression of the person.

3. What is the difference between the system time and the current date?

System time exactly that should be
System.currenttimemillis ();
The new date () is the current date, although it gettime (); and System.currenttimemillis ();
Same, but System.currenttimemillis ();

4. How do I calculate the number of days difference for two dates?

Long begintime = Begindate.gettime ();
Long endTime2 = Enddate.gettime ();
Long betweendays = (long) (Endtime-begintime)/(1000 * 60 * 60 *24) + 0.5);

5. How to compare date time size?

The first method:
Use the Calendar object to compare
Java.util.Calendar class can is used to compare date. In the order to does this,
You guy should parse so string into int year, month, day and construct a
Calendar object, and then do comparison.

Below is a sample

StringTokenizer token = new StringTokenizer (your string, "-");
int year = Integer.parseint (Token.nexttoken ());
int month = Integer.parseint (Token.nexttoken ());
int day = Integer.parseint (Token.nexttoken ());
Calendar date = Calendar.getinstance ();
Date.set (Year,month,day);
Calendar today = Calendar.getinstacne ();
if (Date.after (today)) {
//......
}
The second method
Date Nowdate=new date ()//current time \ R
Long Nowtime=nowdate.gettime;
Long Lasttime=usertime.longvalue ()//previous time \ r
Long time=nowtime-lasttime;//time subtraction comparison.
if (time> (long) 60000)//1 minutes {}

You can also use the following references
With a timestamp, date.gettime () can change the current time to a timestamp,
with CompareTo ();
With before (), after (), Equals ();

6. Format date issue \ r

Objective:
The first time to ask for a date
Java.text.SimpleDateFormat formatter = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
String Riqi=formatter.format (currenttime_1);
Second time of request \ r
Java.text.DateFormat format1 = new Java.text.SimpleDateFormat ("Hhmmss");
Java.util.Date currenttime_2 = new Java.util.Date ();
String Shijian=format1.format (currenttime_2);
The result is
2002-02-19 and 115324 (11 points 53 minutes 24 seconds)

Realize:
Java.text.SimpleDateFormat formatter = new Java.text.SimpleDateFormat ("Yyyy-mm-dd-h-mm-ss");
Java.util.Date currenttime_1 = new Java.util.Date ();
String str_date = Formatter.format (currenttime_1);
StringTokenizer token = new StringTokenizer (str_date, "-");
String year = Token.nexttoken ();
String month= Token.nexttoken ();
String day = Token.nexttoken ();
String hh = Token.nexttoken ();
String mm = Token.nexttoken ();
String ss = Token.nexttoken ();
String riqi=year+ "Years \" +month+ "month" +day+ "Day" + "+hh+" point \ "+mm+" "" +ss+ "" Seconds \ "";
String Newdir=year+month+day;
String Wenjian = Hh+mm+ss;

7. How to get the one-month days?

Java.util.Calendar date = Java.util.Calendar.getInstance ();
System.out.println (Date.getactualmaximum) (date. Day_of_month));




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.