Java create Date and time Notes

Source: Internet
Author: User
Tags current time date1 dateformat getmessage min

(1) public Date ()

The date and time of the created date class object are set to the date and time corresponding to the creation time.

Example:

Java code

1. Date today = new Date (); // today is set to the Date time corresponding to the creation time.
(2) public Date (long date)

The long parameter date can be obtained by calling the static method parse (String s) in the Date class.

Example:

Java code

1. long l = Date. parse ("Mon 6 Jan 1997 ");
2. Date day = new Date (l); // The Middle time of day is Monday, January 1, January 6, 1997.

(3) public Date (String s)

Generates a date object by string s. The format of s is the same as that of the string parameter in method parse.

Example:

Java code

1. Date day = new Date ("Mon 6 Jan 1997"); // The time in day is Monday, January 1, January 6, 1997.
 

(4) by: year, month, and day

Java code
1. public Date (int year, int month, int date)
 

(5) by year, month, day, hour, and minute

Java code

1. public Date (int year, int month, int date, int hrs, int min)

(6) by year, month, day, hour, minute, and second

Java code

1. public Date (int year, int month, int date, int hrs, int min, int sec)
 

Create a date object based on the given parameters.

The value of year is: the year to be set-1900. For example, if the year to be set is 1997, the value of year should be 97, that is, the result of 1997-1900. Therefore, the minimum year in Date can be set to 1900;

The value range of month is 0 ~ Represents, and 11 represents;

The value range of date is 1 ~ Between 31;

The value range of hrs is 0 ~ Between 23. From midnight to one o'clock P.M. the next day, hrs = 0, and from noon to hrs = 12;

The value range of min and sec is 0 ~ Between 59.


The following are some examples.

Create a date object:

The code is as follows: Copy code
Import java. util. Date;
Public class DateExample1 {
Public static void main (String [] args ){
// Get the system date/time
Date date = new Date ();
System. out. println (date. getTime ());
}}

Custom Date data format:

The code is as follows: Copy code

Import java. text. SimpleDateFormat;
Import java. util. Date;
Public class DateExample2 {
Public static void main (String [] args ){
SimpleDateFormat bartDateFormat = new SimpleDateFormat ("EEEE-MMMM-dd-yyyy ");
Date date = new Date ();
System. out. println (bartDateFormat. format (date ));
}}

Parse text data into a date object:

The code is as follows: Copy code

Import java. text. SimpleDateFormat;
Import java. util. Date;
Public class DateExample3 {
Public static void main (String [] args ){
// Create a date formatter that can parse dates
// The form MM-dd-yyyy.
SimpleDateFormat bartDateFormat = new SimpleDateFormat ("MM-dd-yyyy ");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001 ";
Try {
// Parse the text version of the date.
// We have to perform the parse method in
// Try-catch construct in case dateStringToParse
// Does not contain a date in the format we are expecting.
Date date = bartDateFormat. parse (dateStringToParse );

// Now send the parsed date as a long value
// To the system output.

System. out. println (date. getTime ());
}
Catch (Exception ex ){
System. out. println (ex. getMessage ());
}
}}

Time comparison:

The code is as follows: Copy code

Import java. text .*;
Import java. util .*;

Public class TimeCompare {

Public static void main (String [] args ){
Boolean flag = isDateBefore ('2017-09-09 12:12:12 ', '2017-09-09 16:00:00 ');
System. out. println (flag );
Flag = isDateBefore ('2017-09-09 01:01:01 ', '2017-09-09 16:00:00 ');
System. out. println (flag );
Flag = isDateBefore ('2017-09-09 01:01:01 ');
System. out. println (flag );
 }

// Determine whether the time date1 is before the time date2
// Time format 16:16:34
Public static boolean isDateBefore (String date1, String date2 ){
Try {
DateFormat df = DateFormat. getDateTimeInstance ();
Return df. parse (date1). before (df. parse (date2 ));
} Catch (ParseException e ){
System. out. print ('[SYS]' + e. getMessage ());
Return false;
  }
 }
// Determine whether the current time is earlier than date2
// Time format 16:16:34
Public static boolean isDateBefore (String date2 ){
Try {
Date date1 = new Date ();
DateFormat df = DateFormat. getDateTimeInstance ();
Return date1.before (df. parse (date2 ));
} Catch (ParseException e ){
System. out. print ('[SYS]' + e. getMessage ());
Return false;
  }
 }

}

How to implement the current time format in java: yyyy-mm-dd hh: mm: ss and 30 minutes after the current time:

The code is as follows: Copy code

Import java. text .*;
Import java. util .*;
Public class L {
Public static void main (String [] args ){
Date date = new Date (System. currentTimeMillis ());
DateFormat df = DateFormat. getDateTimeInstance (DateFormat. MEDIUM, DateFormat. MEDIUM, Locale. CHINA );
String dt = df. format (date );
System. out. println (dt );
Date = new Date (System. currentTimeMillis () + 30*60*1000); // half an hour later
Dt = df. format (date );
System. out. println (dt );
}}

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.