Example of a time class usage in Java Date,timestamp,dateformat

Source: Internet
Author: User
Tags date1 dateformat

We will analyze the time-date class and the processing functions in Java itself.

A class related to the time date.

  java.util.Date . Implements a class whose objects have time, date components.

  Java.util.Calendar . An abstract class whose objects have time, date components.

  java.sql.Date . Implements a class whose object has a date component.

  Java.sql.Time . Implements a class whose objects have a time component.

  Java.sql.Timestamp . Implements a class whose objects have a time-date component.

  Java.text.DateFormat . An abstract class whose objects are formatted with a time date.


We generally use classes java.util.Date,java.util.Calendar,java.sql.Timestamp, Java.text.DateFormat Perform time-date operations because they have full time-date components and comprehensive formatting capabilities. It is worth noting that java.sql.Date does not have a time component, and java.sql.Time does not have a date component !

Here are some examples of usage

public static void Main (string[] args) {/*** shows the output type of each datetime component */java.sql.date sqldate = new Java.sql.Date ( System.currenttimemillis ()); System.out.println (Sqldate.tostring ()); Output result: 2015-06-25java.sql.time sqltime = new Java.sql.Time (System.currenttimemillis ()); System.out.println (Sqltime.tostring ()); Output result: 09:13:43java.sql.timestamp Sqltimestamp = new Java.sql.Timestamp (System.currenttimemillis ()); System.out.println (Sqltimestamp.tostring ()); Output result: 2015-06-25 09:13:43.561java.util.date utildate = new Java.util.Date (System.currenttimemillis ()); System.out.println (Utildate.tostring ()); Output result: Thu June 09:13:43 CST 2015java.util.calendar cl = java.util.Calendar.getInstance (); System.out.println (Cl.gettime (). toString ()); Output result: Thu June 09:13:43 CST 2015}


public static void Main (string[] args) {/** * java.sql.Timestamp usage, */@SuppressWarnings ("deprecation") Java.sql.Timestamp sqlTimestamp1 = new Java.sql.Timestamp (2015,6,25,9,13,43,561); Java.sql.Timestamp Sqltimestamp = New Java.sql.Timestamp (System.currenttimemillis ()); System.out.println (Sqltimestamp); Boolean isafter = Sqltimestamp.after (SQLTIMESTAMP1); System.out.println (isafter);    Falseboolean Isbefore = Sqltimestamp.before (SQLTIMESTAMP1); System.out.println (Isbefore);   true//is used to compare the length of two time points, in milliseconds long gettimes = Sqltimestamp.gettime (); System.out.println (gettimes);   Output: 1435196988250//convert String type to Timestamp type Java.sql.Timestamp SQLTIMESTAMP2 = java.sql.Timestamp.valueOf ("2015-06-25 09:48:16.524 "); System.out.println (SQLTIMESTAMP2);   Output: 2015-06-25 09:48:16.524}


public static void Main (string[] args) {/** * Java.text.SimpleDateFormat usage */simpledateformat df = new SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "); Timestamp now = new Timestamp (System.currenttimemillis ()); System.out.println (now); 2015-06-25 14:27:41.477string time = Df.format (now); System.out.println (time); 2015-06-25 14:27:41system.out.println (timestamp.valueof (time)); 2015-06-25 14:27:41.0date now1 = new Date (); System.out.println (now); 2015-06-25 14:27:41.477string time1 = Df.format (NOW1); System.out.println (TIME1); 2015-06-25 14:27:41system.out.println (timestamp.valueof (time1)); 2015-06-25 14:27:41.0}


public static void Main (string[] args) {/** * Java.util.Calendar usage */calendar calendar1 = calendar.getinstance (); System.out.println (CALENDAR1); java.util.gregoriancalendar[time=1435214975097,arefieldsset=true,//areallfieldsset=true,lenient=true,zone= Sun.util.calendar.zoneinfo[id= "Asia/shanghai",//offset=28800000,dstsavings=0,usedaylight=false,transitions=19, lastrule=null],//firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=2015,month=5,//WEEK_OF_YEAR=26,WEEK_OF_ month=4,day_of_month=25,day_of_year=176,day_of_week=5,day_of_week_in_month=4,//AM_PM=1,HOUR=2,HOUR_OF_DAY=14, minute=49,second=35,millisecond=97,zone_offset=28800000,dst_offset=0]//Get time Date date1 = Calendar1.gettime (); System.out.println (date1); Thu June 14:49:35 CST 2015system.out.println (Calendar1.getweeksinweekyear ()); 52system.out.println (Calendar1.get (calendar.day_of_month)); 25system.out.println (Calendar1.get (calendar.hour_of_day)); 14}


Example of a time class usage in Java Date,timestamp,dateformat

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.