Java obtains the timestamp.

Source: Internet
Author: User

Java obtains the timestamp.

JAVA obtains the timestamp of the initial time of the current month.

  1. Public static long getMonthFirstDay (){
  2. Calendar calendar = Calendar. getInstance (); // get the current date
  3. Calendar. add (Calendar. MONTH, 0 );
  4. Calendar. set (Calendar. DAY_OF_MONTH, 1); // set it to 1. The current date is the first day of the month.
  5. Calendar. set (Calendar. HOUR_OF_DAY, 0 );
  6. Calendar. set (Calendar. MINUTE, 0 );
  7. Calendar. set (Calendar. SECOND, 0 );
  8. System. out. println (calendar. getTimeInMillis ());
  9. Return calendar. getTimeInMillis ();
  10. }
Obtain the current Timestamp
// Method 1 System. currentTimeMillis (); // method 2 Calendar. getInstance (). getTimeInMillis (); // method 3 new Date (). getTime ();
Calendar. getInstance (). getTimeInMillis () is the slowest speed, because Canlendar takes a lot of time to handle time zone issues.

In the development process, many people usually get used to using new Date () to obtain the current time, which is convenient to use. At the same time, they can also obtain information about the current time, for example, you can get the hour, minute, and so on. You can also format the output, which contains a wealth of information. But sometimes you don't need to get that much information, you just need to care about the number of milliseconds it returns, for exampleGetTime(). In order to get the timestamp, many people also like to use new Date (). getTime () to get it. At first glance, there is no problem, but it is not necessary.

In fact, let's take a look at the java source code:

  public Date()  {    this(System.currentTimeMillis());  }

Obviously, what new Date () does is actually calling System. currentTimeMillis (). If only the number of milliseconds is required, you can use System. currentTimeMillis () instead of new Date (), which is more efficient. In addition, many people prefer to use new Date () multiple times in the same method. Generally, the performance is consumed at 1.1 points. In fact, a reference can be declared here.

Get current time
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // you can specify the date format.
String date = df. format (new Date (); // new Date () is used to obtain the current system time. You can also use the current timestamp.

Let's take a look at several JAVA methods to get the current time

Package com. xjp. common. util;

Import java. SQL. Timestamp;

Import java. text. ParsePosition;

Import java. text. SimpleDateFormat;

Import java. util. Date;

Import com. ttsoft. framework. util. DateUtil;

/**

* Title: Time Acquisition

* Description: Current Time

* Company:

* @ Author jiq

* @ Version 1.0

*/

Public class XJPDateUtil extends DateUtil {

Public static final String [] months = {"August", "August ",

"August ",};

Public static final String [] quarters = {"first quarter", "second quarter", "third quarter", "fourth quarter "};

Public XJPDateUtil (){

}

/**

* Obtain the date string.

*

*

* Date string format: yyyyMMdd

* Where:

* Yyyy indicates a four-digit year.

* MM indicates a two-digit month.

* Dd indicates a two-digit day.

*

*

* @ Return String "yyyyMMdd" format date String.

*/

Public static String getDate (){

SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd ");

Return formatter. format (new Date ());

}

/**

* Obtain the string of the current year.

*

*

* Date string format: yyyy

* Where:

* Yyyy indicates a four-digit year.

*

*

* @ Return String "yyyy" is a String of the current year.

*/

Public static String getNowYear (){

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy ");

Return formatter. format (new Date ());

}

/**

* Obtain the current monthly string.

*

*

* Date string format: MM

* Where:

* MM indicates a four-digit year.

*

*

* @ Return String "yyyy" indicates the current monthly String.

*/

Public static String getNowMonth (){

SimpleDateFormat formatter = new SimpleDateFormat ("MM ");

Return formatter. format (new Date ());

}

/**

* Obtain the current monthly string.

*

*

* Date string format: dd

* Where:

* Dd indicates a four-digit year.

*

*

* @ Return String "yyyy" indicates the current monthly String.

*/

Public static String getNowDay (){

SimpleDateFormat formatter = new SimpleDateFormat ("dd ");

Return formatter. format (new Date ());

}

/**

* Obtain the date string.

*

*

* Date string format: yyyyMMdd

* Where:

* Yyyy indicates a four-digit year.

* MM indicates a two-digit month.

* Dd indicates a two-digit day.

*

*

* @ Param date

* Date to be converted.

* @ Return String "yyyyMMdd" format date String.

*/

Public static String getDate (Date date ){

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");

Return formatter. format (date );

}

/**

* Obtain the date string.

*

*

* Date string format: yyyyMMdd

* Where:

* Yyyy indicates a four-digit year.

* MM indicates a two-digit month.

* Dd indicates a two-digit day.

*

*

* @ Param date

* Date to be converted.

* @ Return String "yyyyMMdd" format date String.

*/

/**

* Convert a specified date string to a date object

*

* @ Param dateStr

* Date string

* @ Return java. util. Date

* @ Roseuid 3f39fe0000385

*/

Public static Date getDate (String dateStr ){

If (XJPTypeChecker. isDate (dateStr) {// Date type

SimpleDateFormat df = new SimpleDateFormat ("yyyyMMdd ");

Try {

Java. util. Date date = df. parse (dateStr );

Return date;

} Catch (Exception ex ){

Logger. write ("the date format does not match or the date is blank! Please check! ");

Return null;

} // End try-catch

} Else if (XJPTypeChecker. isDatetime (dateStr) {// datetime type

SimpleDateFormat df = new SimpleDateFormat (

"Yyyy-MM-dd HH: mm: ss. SSS ");

Try {

Java. util. Date date = df. parse (dateStr );

Return date;

} Catch (Exception ex ){

Return null;

} // End try-catch

} // End if

Return null;

}

/**

* Obtain the date string.

*

*

* Date string format: yyyy-MM-dd

* Where:

* Yyyy indicates a four-digit year.

* MM indicates a two-digit month.

* Dd indicates a two-digit day.

*

*

* @ Return String "yyyy-MM-dd" format date String.

*/

Public static String getHyphenDate (){

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");

Return formatter. format (new Date ());

}

/**

* Obtain the date string.

*

*

* Date string format: yyyy-MM-dd

* Where:

* Yyyy indicates a four-digit year.

* MM indicates a two-digit month.

* Dd indicates a two-digit day.

*

*

* @ Param date

* Date to be converted.

* @ Return String "yyyy-MM-dd" format date String.

*/

Public static String getHyphenDate (Date date ){

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");

Return formatter. format (date );

}

/**

* Convert a date string in the format of "yyyyMMdd" to a date object.

*

* @ Param source

* Date string.

* @ Return Date object.

*/

Public static Date parsePlainDate (String source ){

SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd ");

Return sdf. parse (source, new ParsePosition (0 ));

}

/**

* Convert a date string in "yyyy-MM-dd" format to a date object.

*

* @ Param source

* Date string.

* @ Return Date object.

*/

Public static Date parseHyphenDate (String source ){

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");

Return sdf. parse (source, new ParsePosition (0 ));

}

/**

* Converts a date string in the specified format to a date object.

*

* @ Param source

* Date string.

* @ Param pattern

* Mode.

* @ Return Date object.

*/

Public static Date parseDate (String source, String pattern ){

SimpleDateFormat sdf = new SimpleDateFormat (pattern );

Return sdf. parse (source, new ParsePosition (0 ));

}

/**

* Convert a date string in "yyyy-MM-dd" format to a date string in "yyyyMMdd" format.

*

* @ Param source

* Date string.

* @ Return String "yyyymmdd" format date String.

*/

Public static String toPlainDate (String source ){

Date date = parseHyphenDate (source );

SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd ");

Return formatter. format (date );

}

/**

* Convert a date string in "yyyyMMdd" format to a date string in "yyyy-MM-dd" format.

*

* @ Param source

* Date string.

* @ Return String "yyyy-MM-dd" format date String.

*/

Public static String toHyphenDate (String source ){

Date date = parsePlainDate (source );

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");

Return formatter. format (date );

}

/**

* Get the timestamp and convert the date object to the timestamp type.

*

* @ Param date

* Date object

* @ Return Timestamp

*/

Public static Timestamp getTimestamp (Date date ){

Return new Timestamp (date. getTime ());

}

/**

* Get the timestamp and convert the current date to the timestamp type.

*

* @ Return Timestamp

*/

Public static Timestamp getTimestamp (){

Return new Timestamp (new Date (). getTime ());

}

/**

* Convert a date string in "yyyyMMdd" format to an object of the Timestamp type.

*

* @ Param source

* Date string

* @ Return Timestamp

*/

Public static Timestamp parseTimestamp (String source ){

Date date = parsePlainDate (source );

Return getTimestamp (date );

}

/**

* Obtain the annual cycle

* Example:

* XJPDateUtil. getPeriodYear ("20040229",-1 );

* XJPDateUtil. getPeriodYear ("20040228",-1 );

* XJPDateUtil. getPeriodYear ("20020230", 2 );

*

* @ Param source

* Time string

* @ Param yearPeriod

* Year cycle-1 indicates the previous year of the current time, and so on.

* @ Return String time.

*/

Public static String getPeriodYear (String source, int yearPeriod ){

Int p = Integer. parseInt (source. substring (0, 4) + yearPeriod;

String newYear = String. valueOf (p)

+ Source. substring (4, source. length ());

Date date = parsePlainDate (newYear );

String s = getDate (date );

Int ny = Integer. parseInt (s );

Int sy = Integer. parseInt (newYear );

While (ny> sy ){

Sy --;

Ny = Integer. parseInt (getDate (parsePlainDate (String. valueOf (sy ))));

}

Return String. valueOf (sy );

}

/**

* Get the current date and time

*

* @ Return String

*/

Public static String getCurrentDateStr (){

Date date = new Date ();

String str = null;

SimpleDateFormat df = new SimpleDateFormat ("yyyyMMdd HH: mm: ss ");

Str = df. format (date );

Return str;

}

/**

* Date Addition

*

* @ Param day

* Days

* @ Return returns the date after the addition.

*/

Public static String addDate (int day ){

Java. util. Calendar c = java. util. Calendar. getInstance ();

C. setTimeInMillis (System. currentTimeMillis () + (long) day) * 24*3600

* 1000 );

SimpleDateFormat df = new SimpleDateFormat ("yyyyMMdd HH: mm: ss ");

Return df. format (c. getTime ());

}

/**

* Returns millisecond

*

* @ Param date

* Date

* @ Return returns millisecond

*/

Public static long getMillis (java. util. Date date ){

Java. util. Calendar c = java. util. Calendar. getInstance ();

C. setTime (date );

Return c. getTimeInMillis ();

}

/**

* Get the current date and time

* @ Param format: yyyy-MM-dd hh: mm

* @ Return String

*/

Public static String getNowDate (String format ){

Date date = new Date ();

String str = null;

SimpleDateFormat df = new SimpleDateFormat (format );

Str = df. format (date );

Return str;

}

/**

* Reduce the strmon date by one month.

* @ Param mon

* @ Return

*/

Public static String getReduceMonDate (String strmon ){

If (strmon! = Null &&! Strmon. equals ("")){

Date mon = parseHyphenDate (strmon );

Mon. setMonth (mon. getMonth ()-1 );

Return getHyphenDate (mon );

} Else {

Return "";

}

}

Public static String getTimeStr (String dateStr ){

Date date = getDate (dateStr );

String str = null;

SimpleDateFormat df = new SimpleDateFormat ("HH: mm: ss ");

Str = df. format (date );

Return str;

}

Public static String getTimeStr (){

String str = "";

SimpleDateFormat df = new SimpleDateFormat ("HH: mm: ss ");

Str = df. format (new Date ());

Return str;

}

}

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.