Processing of Java Time

Source: Internet
Author: User

1. Java computing time depends on the number of milliseconds that start January 1, 1970.
2. The date class's constructor date () returns an object that represents the moment that is currently created. The method of date gettime () returns a Long value in the number of times before or after January 1, 1970.
3. The DateFormat class is used to convert date to string and vice versa. The static method Getdateinstance () returns the default format of the DateFormat; Getdateinstance (Dateformat.field) returns the specified DateFormat object format. The Format (Date D) method returns a String that represents a date, such as "January 1,2002." In turn, the parse (string s) method returns a Date object that is represented by a parameter string.
4. The format of the string returned by the format () method differs depending on the time settings in the different regions.
5. The Gregoriancalendear class has two important constructors: Gregoriancalerdar (), which returns the object representing the current creation time; GregorianCalendar (int year,int month,int date) Returns an object that represents an arbitrary date. The GetTime () method of the GregorianCalendar class returns a Date object. The Add (int field,int amount) method calculates the date by adding or decreasing the time unit, such as the number of days, months, or years.
GregorianCalendar and Time
Two GregorianCalendar constructors can be used to process the time. The former creates an object that represents the date, hour, and minute:
GregorianCalendar (int year, int month, int date, int hour, int minute)
The second creates a representation of a date, hour, minute, and second:
GregorianCalendar (int year, int month, int date, int hour, int minute, int second)
First, I should remind you that each constructor requires the date information in the time information (year, month, day). If you want to say "p.m.", you must point out the date.
Similarly, each GregorianCalendar constructor creates an object that is computed on a time-using millisecond. So, if your constructor provides only the year, month, day parameter, that hour, minute, second, and millisecond value will be set to 0.
DateFormat and Time
You can use the static method getdatetimeinstance (int datestyle,int timestyle) to create a DateFormat object to display the time and date. This method indicates the date and time format you want. If you prefer to use the default format, you can use Getdatetimeinstance () instead.
You can use the static method gettimeinstance (int timestyle) to create the DateFormat object to display the correct time.
The following procedure demonstrates how Getdatetimeinstance () and Gettimeinstance () work:
Import java.util.*;
Import java.text.*;
public class Apollo {
public static void Main (string[] args) {
GregorianCalendar liftOffApollo11 = new GregorianCalendar (1969, Calendar.july, 16, 9, 32);
Date d = liftoffapollo11.gettime ();
DateFormat df1 = dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);
DateFormat DF2 = dateformat.gettimeinstance (Dateformat.short);
String S1 = Df1.format (d);
String s2 = Df2.format (d);
System.out.println (S1);
SYSTEM.OUT.PRINTLN (S2);
}
}
On my computer, the above program shows the following:
Jul, 1969 9:32:00 AM
9:32 AM
(The output is different depending on your location)
Calculate time interval
You may sometimes need to calculate the time of the past; For example, give you the start and end times, and you want to know the duration of the manufacturing process. A rental company rents things by hours or days, and calculating time is also useful for them. Similarly, in the financial world, it is often necessary to calculate important payment times.
Complicating the problem, humans are at least two ways of calculating time. You can say that the day is over when 24 hours have passed, or the calendar turns from today to tomorrow. We'll discuss both of the things we think about.
Time period, Case 1: Strict time unit
In this case, only 24 hours past, this genius past, 60 minutes past, this hour is past, 60 seconds past, this minute is past, and so on. In this method, 23 hours of time will be considered 0 days.
Using this method to calculate the time period, you start by calculating the millisecond of the past. To do this, first convert each date to a number of milliseconds from January 1, 1970 onwards. You can subtract the first millisecond value from a second millisecond value. Here's a simple calculation:
Import java.util.*;
public class Elapsedmillis {
public static void Main (string[] args) {
GregorianCalendar GC1 = new GregorianCalendar (1995, 11, 1, 3, 2, 1);
GregorianCalendar GC2 = new GregorianCalendar (1995, 11, 1, 3, 2, 2);
The above dates is one second apart
Date D1 = Gc1.gettime ();
Date D2 = Gc2.gettime ();
Long L1 = D1.gettime ();
Long L2 = D2.gettime ();
Long difference = L2-L1;
System.out.println (' Elapsed milliseconds: ' + difference);
}
}
The above program prints as follows:
Elapsed milliseconds:1000
This program also brings a bit of confusion. The gettime () of the GregorianCalendar class returns a Date object, and the date class's GetTime () method returns the millisecond value of a long type from January 1, 1970 to this time. Although their method names are the same, the return values are different!
The following program fragment converts milliseconds to seconds with a simple integer division:
Long milliseconds = 1999;
Long seconds = 1999/1000;
This method converts the fractional part into milliseconds to seconds, so 1,999 milliseconds equals 1 seconds, and 2,000 milliseconds equals 2 seconds.
Calculate larger units-such as days, hours, and minutes-given a time value, you can use the following procedure:
1. Calculate the maximum unit, minus the number of seconds
2. Calculate the second largest unit, minus the number of seconds
3. Repeat until only seconds are left
For example, if your time is 10,000 seconds, you want to know how many hours, how many minutes, and how many seconds you start with the largest unit: hours. 10,000 divided by 3600 (the number of seconds in one hours) to get the number of hours. With integer division, the answer is 2 hours (fractional rounding in integer division) to calculate the number of seconds left, 10,000-(3,600 x 2) = 2,800 seconds. So you have 2 hours and 2,800 seconds.
Convert the 2,800-second component clock, 2,800 divided by 60. Using integer division, the answer is 46. 2,800-(x 46) = 40 seconds. The final answer is 2 hours, 46 minutes, 40 seconds.
The following Java program uses the above calculation method:
Import java.util.*;
public class Elapsed1 {
public void calchms (int timeinseconds) {
int hours, minutes, seconds;
Hours = timeinseconds/3600;
TimeInSeconds = timeinseconds-(hours * 3600);
minutes = TIMEINSECONDS/60;
TimeInSeconds = TimeInSeconds-(minutes * 60);
seconds = timeinseconds;
System.out.println (Hours + ' hour (s) ' + minutes + ' minute (s) ' + Seconds + ' second (s) ');
}
public static void Main (string[] args) {
Elapsed1 elap = new Elapsed1 ();
ELAP.CALCHMS (10000);
}
}
The output results are as follows:
2 hour (s) minute (s) second (s)
The above program can calculate the number of hours correctly even in less than one hours. For example, you use the above program to calculate 1,000 seconds, the output into the following:
0 hour (s) minute (s) second (s)
To give a real-world example, the following program calculates the time that Apollo 11 flew to the moon:
Import java.util.*;
public class Lunarlanding {
Public long Getelapsedseconds (GregorianCalendar gc1, GregorianCalendar gc2) {
Date D1 = Gc1.gettime ();
Date D2 = Gc2.gettime ();
Long L1 = D1.gettime ();
Long L2 = D2.gettime ();
Long difference = Math.Abs (L2-L1);
return difference/1000;
}
public void Calchm (long timeinseconds) {
Long hours, minutes, seconds;
Hours = timeinseconds/3600;
TimeInSeconds = timeinseconds-(hours * 3600);
minutes = TIMEINSECONDS/60;
System.out.println (Hours + ' hour (s) ' + minutes + ' minute (s) ');
}
public static void Main (string[] args) {
GregorianCalendar lunarlanding = new GregorianCalendar (1969, Calendar.july, 20, 16, 17);
GregorianCalendar lunardeparture = new GregorianCalendar (1969, Calendar.july, 21, 13, 54);
GregorianCalendar Starteva = new GregorianCalendar (1969, Calendar.july, 20, 22, 56);
GregorianCalendar Endeva = new GregorianCalendar (1969, Calendar.july, 21, 1, 9);
lunarlanding apollo = new lunarlanding ();
Long Eva = Apollo.getelapsedseconds (Starteva, Endeva);
System.out.print (' EVA duration = ');
Apollo.calchm (EVA);
Long Lunarstay = Apollo.getelapsedseconds (lunarlanding, lunardeparture);
System.out.print (' Lunar stay = ');
Apollo.calchm (Lunarstay);
}
}
The above program output is as follows:
EVA Duration = 2 hour (s) minute (s)
Lunar stay = h

Processing of Java Time

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.