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

Source: Internet
Author: User
Tags date1 dateformat time and date

We will analyze it with the time date class in Java and the processing functions in it.

A class that is related to time and date.

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

Java.util.Calendar. 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 object has a time component.

Java.sql.Timestamp. Implements a class whose object has a time-date component.

Java.text.DateFormat. An abstract class whose object formats the time date.


We will generally use the class java.util.Date, Java.util.Calendar, Java.sql.Timestamp, Java.text.DateFormat do time and date operations because they have full time and date components and full formatting capabilities. It is noteworthy that Java.sql.Date does not have a time component, and that Java.sql.Time does not have a date component.

Here are examples of usage

public static void Main (string[] args) {
		/**
		*	shows the output type of each date-time component *
		*
		java.sql.Date sqldate = new Java.sql.Date (System.currenttimemillis ());
		System.out.println (Sqldate.tostring ()); Output result: 2015-06-25
		
		java.sql.Time sqltime = new Java.sql.Time (System.currenttimemillis ());
		System.out.println (Sqltime.tostring ()); Output result: 09:13:43
		
		java.sql.Timestamp sqltimestamp = new Java.sql.Timestamp (System.currenttimemillis ());
		System.out.println (Sqltimestamp.tostring ()); Output result: 2015-06-25 09:13:43.561
		
		java.util.Date utildate = new Java.util.Date (System.currenttimemillis ());
		System.out.println (Utildate.tostring ()); Output result: Thu June 09:13:43 CST 2015
		
		Java.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);    False
	
	Boolean Isbefore = Sqltimestamp.before (SQLTIMESTAMP1);
	System.out.println (Isbefore);   True
	//is used to compare the length of two points, in unit 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.477
		String time = Df.format (now);
		System.out.println (time); 2015-06-25 14:27:41
		System.out.println (timestamp.valueof (time));//2015-06-25 14:27:41.0

		Date now1 = new Date ();
		System.out.println (now); 2015-06-25 14:27:41.477
		String time1 = Df.format (NOW1);
		System.out.println (TIME1); 2015-06-25 14:27:41
		System.out.println (timestamp.valueof (time1));//2015-06-25 14:27:41.0

	}


public static void Main (string[) args) {/** * Java.util.Calendar usage * * Calendar CALENDAR1 = Calen
	Dar.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 2015 System.out.println (Calendar1.getweeksinweekyear ()); System.out.println (Calendar1.get (calendar.day_of_month)); System.out.println (Calendar1.get (calendar.hour_of_day)); } 


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.