Java Date processing summary (ii)

Source: Internet
Author: User
Tags dateformat time zones

1.java.sql.timestamp

The specific time we take from the database is typically java.sql.Timestamp type. Java.sql.Timestamp is a subclass of Java.util.Date. Java.sql.Timestamp contains dates and times, and Java.sql.Date contains only dates.

It adds TIMESTAMP the ability to save the SQL fractional seconds value by allowing fractional seconds to the specification of nanosecond precision.

The accuracy of the computed Timestamp object is either:

    • 19 , which is the number of characters in the YYYY-MM-DD hh:mm:ss format
    • 20 + s , which is the number of characters in Yyyy-mm-dd hh:mm:ss.[fff ...] format, which s represents the scale of a given Timestamp (its fractional seconds precision).

This type consists java.util.Date of a separate nanosecond value. only integer seconds are stored in the java.util.Date component. Fractional seconds (nanoseconds) exist independently . java.sql.Timestampwhen you pass an object that is not an instance, the Timestamp.equals(Object) method never returns true , because the nanosecond component of the date is unknown. Therefore, the java.util.Date.equals(Object) method is asymmetric relative to the method Timestamp.equals(Object) . In addition, the hashcode method uses the underlying java.util.Date implementation and therefore does not include nanoseconds in its calculations.

Because Timestamp of the difference between a class and the above java.util.Date class, it is recommended that the code generally not treat Timestamp the value as java.util.Date an instance . Timestampthe java.util.Date inheritance relationship between and is actually the implementation of inheritance, not type inheritance.

  The ToString and ValueOf methods use the JDBC timestamp escape format to orchestrate the timestamp. yyyy-mm-dd hh:mm:ss.fffffffff, which ffffffffff indicates the nanosecond.

2.java.sql.time

This class is similar to Java.sql.Date, which represents time.

Common methods:

 

Time(long time)
Constructs an object using millisecond time values Time .

 String toString()
Time is formatted using the JDBC time escape format.
static Time valueOf(String s)
Converts a string that uses the JDBC time escape format to a Time value.

the time character format returned is: Hh:mm:ss

3.java.sql.calendar

The Calendar class is not very much, it is an abstract class , we often use getinstance() to get its object, or use its subclasses Java.util.GregorianCalendar. It can take out a certain part of the date (such as year, month, day, time, minute, second, etc.). Here's how to use the calendar.

Use the Get () method in the calendar to remove the desired part;

The following constants are commonly used in the calendar

For date fields:

 
 

For TIME fields:

Hour_of_day am_pm + HOUR

Excerpted from http://blog.csdn.net/joyous/article/details/9630893

1  PackageMyTest;2 3 /*4 * The general operation of the Calendar is shown5 */6 Importjava.util.Date;7 ImportJava.text.SimpleDateFormat;8 ImportJava.text.DateFormat;9 ImportJava.util.Calendar;Ten  One  Public classTest A { -  PublicTest () - { the } -  -  Public Static voidMain (string[] args) - { + //string Conversion date format -DateFormat Fmtdatetime =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); + //Get date format Objects ADate Date =Fmtdatetime.parse (strdatemake); at  - //Full display date time -String str = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (NewDate ()); - System.out.println (str); -  - //Create a Calendar object inCalendar Calendar =calendar.getinstance (); - //Initialize Calendar object, but not necessary unless reset time is required toCalendar.settime (NewDate ()); +  - //settime Similar to the above line the //Date date = new Date (); * //calendar.settime (date); $ Panax Notoginseng //Show Year - intYear =Calendar.get (calendar.year); theSystem.out.println ("Year is =" +string.valueof (year)); +  A //Display month (starting from 0, actual display to add one) the intMONTH =Calendar.get (calendar.month); +SYSTEM.OUT.PRINTLN ("Month is =" + (month + 1)); -  $ //the nth day of the year $ intDay_of_year =Calendar.get (calendar.day_of_year); -System.out.println ("day_of_year is =" +day_of_year); -  the //Nth day of the month - intDay_of_month =Calendar.get (calendar.day_of_month);WuyiSystem.out.println ("Day_of_month =" +string.valueof (Day_of_month)); the  - //after 3 hours WuCalendar.add (Calendar.hour_of_day, 3); - intHour_of_day =Calendar.get (calendar.hour_of_day); AboutSystem.out.println ("Hour_of_day + 3 =" +hour_of_day); $  - //current number of minutes - intMINUTE =Calendar.get (calendar.minute); -System.out.println ("MINUTE =" +MINUTE); A  + //15 minutes later . theCalendar.add (Calendar.minute, 15); -MINUTE =Calendar.get (calendar.minute); $System.out.println ("MINUTE + 15 =" +MINUTE); the  the //30 minutes ago theCalendar.add (Calendar.minute, 30); theMINUTE =Calendar.get (calendar.minute); -System.out.println ("MINUTE-30 =" +MINUTE); in  the //formatted display thestr = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS") . Format (Calendar.gettime ()); About System.out.println (str); the  the //Reset Calendar Show Current Time theCalendar.settime (NewDate ()); +str = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS") . Format (Calendar.gettime ()); - System.out.println (str); the Bayi //Create a Calendar to compare time theCalendar calendarnew =calendar.getinstance (); the  - //set to 5 hours ago, the latter large, show-1 -Calendarnew.add (Calendar.hour, 5); theSystem.out.println ("Time Comparison:" +Calendarnew.compareto (Calendar)); the  the //set 7 hours later, the former large, showing 1 theCalendarnew.add (Calendar.hour, +7); -System.out.println ("Time Comparison:" +Calendarnew.compareto (Calendar)); the  the //return 2 hours, same time, display 0 theCalendarnew.add (Calendar.hour, 2);94System.out.println ("Time Comparison:" +Calendarnew.compareto (Calendar)); the } the}

4. The GMT

Under Popular Science, the following concepts can be considered the same:

 Greenwich Mean Time (GMT) = = World Time (UT) = = Universal Time (UTC)

  The time we get is the time that the computer gets the default time zone from the system, so how do we look at the current time in other time zones?

In Java,Java.util.TimeZone is a time zone class that uses the Timezone.gettimezone (String ID) to obtain the time zone for the corresponding ID.

Example:

1 ImportJava.util.Calendar;2 ImportJava.util.GregorianCalendar;3 ImportJava.util.TimeZone;4 5 6  Public classTestut {7 8      Public Static voidMain (string[] args) {9Calendar c=NewGregorianCalendar (Timezone.gettimezone ("Japan")));Ten System.out.println (C.get (Calendar.hour_of_day)); One  A     } -  -}

So how do we know what the ID is? With Timezone.getavailableids (), you can get the ID you can use

1 ImportJava.util.TimeZone;2 3  Public classTestut {4 5      Public Static voidMain (string[] args) {6          for(String s:timezone.getavailableids ()) {7 System.out.println (s);8         }9     }Ten  One}

5. Get the current time
    • System.currenttimemillis (); Often used to test code efficiency and calculate run time.
    • New Date (); Converts the current time to a Date object.
    • Calendar.getinstance ();

Java Date processing summary (ii)

Related Article

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.