How Java Gets the timestamp

Source: Internet
Author: User
Tags string format

JAVA gets the timestamp of the current month's initial time

  1. public static long Getmonthfirstday () {
  2. Calendar calendar = calendar.getinstance ();//Get current date
  3. Calendar.add (calendar.month, 0);
  4. Calendar.set (Calendar.day_of_month, 1);//set to number 1th, 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. }
Gets the current time stamp
Method one System.currenttimemillis ();//Method two Calendar.getinstance (). Gettimeinmillis ();//Method three new Date (). GetTime ();
Calendar.getinstance (). Gettimeinmillis () This is the slowest, because it takes more time for the Canlendar to handle time zone problems.

In the development process, often many people are accustomed to use the new date () to get the current time, it is more convenient to use, but also to get the current time related to all aspects of information, such as get hours, minutes and so on, but also can format the output, contains the information is relatively rich. But sometimes you don't need to get that much information, you just have to care about the number of milliseconds it returns, such as getTime(). In order to get this timestamp, many people also like to use the new date (). GetTime () to get, I see no problem, but in fact there is no need.

In fact, look at the Java source code will know:

  Public Date ()  {This    (System.currenttimemillis ());  }

It's already obvious that what new Date () is doing is actually calling System.currenttimemillis (). If it is just a need or a millisecond, then you can use System.currenttimemillis () instead of the new Date (), which is a bit more efficient. And many people like to use the new Date () multiple times in the same method, usually the performance is 1.1 points to consume, here can actually declare a reference.

Get current time
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
String date = Df.format (new date ()), or new date () to get the current system time, or to use the current timestamp

Let's take a look at some of the ways Java gets 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 = {"January", "February", "March", "April", "May", "June",

"July", "August", "September", "October", "November", "December",};

public static final string[] Quarters = {"One quarter", "Two Quarter", "Three Quarter", "Four Quarter"};

Public Xjpdateutil () {

}

/**

* Gets the date string.

*

*

* Date string format: YYYYMMDD

which

* YYYY represents 4-digit years.

* MM indicates 2-bit months.

* DD denotes 2-digit day.

*

*

* @return string "YYYYMMDD" in the format of date strings.

*/

public static String GetDate () {

SimpleDateFormat formatter = new SimpleDateFormat ("YyyyMMdd");

Return Formatter.format (New Date ());

}

/**

* Gets the current year string.

*

*

* Date string format: yyyy

which

* YYYY represents 4-digit years.

*

*

* @return string of the current year in string "yyyy" format.

*/

public static String Getnowyear () {

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy");

Return Formatter.format (New Date ());

}

/**

* Gets the current monthly string.

*

*

* Date string format: MM

which

* MM represents 4-digit years.

*

*

* @return The current monthly string in string "yyyy" format.

*/

public static String Getnowmonth () {

SimpleDateFormat formatter = new SimpleDateFormat ("MM");

Return Formatter.format (New Date ());

}

/**

* Gets the current monthly string.

*

*

* Date string format: DD

which

* DD denotes 4-digit year.

*

*

* @return The current monthly string in string "yyyy" format.

*/

public static String Getnowday () {

SimpleDateFormat formatter = new SimpleDateFormat ("DD");

Return Formatter.format (New Date ());

}

/**

* Gets the date string.

*

*

* Date string format: YYYYMMDD

which

* YYYY represents 4-digit years.

* MM indicates 2-bit months.

* DD denotes 2-digit day.

*

*

* @param date

* Date of conversion required.

* @return string "YYYYMMDD" in the format of date strings.

*/

public static String getDate (date date) {

SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");

return Formatter.format (date);

}

/**

* Gets the date string.

*

*

* Date string format: YYYYMMDD

which

* YYYY represents 4-digit years.

* MM indicates 2-bit months.

* DD denotes 2-digit day.

*

*

* @param date

* Date of conversion required.

* @return string "YYYYMMDD" in the format of date strings.

*/

/**

* Converts the specified date string to a Date object

*

* @param datestr

* Date String

* @return Java.util.Date

* @roseuid 3f39fe450385

*/

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 ("date format does not match or the date is empty!!!");

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;

}

/**

* Gets the date string.

*

*

* Date string format: YYYY-MM-DD

which

* YYYY represents 4-digit years.

* MM indicates 2-bit months.

* DD denotes 2-digit day.

*

*

* @return string "YYYY-MM-DD" in the format of date strings.

*/

public static String Gethyphendate () {

SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");

Return Formatter.format (New Date ());

}

/**

* Gets the date string.

*

*

* Date string format: YYYY-MM-DD

which

* YYYY represents 4-digit years.

* MM indicates 2-bit months.

* DD denotes 2-digit day.

*

*

* @param date

* Date of conversion required.

* @return string "YYYY-MM-DD" in the format of date strings.

*/

public static String gethyphendate (date date) {

SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");

return Formatter.format (date);

}

/**

* Convert date strings in "YYYYMMDD" format to date objects.

*

* @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 date strings in "YYYY-MM-DD" format to date objects.

*

* @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 of 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 date strings in "YYYY-MM-DD" format to date strings in "YYYYMMDD" format.

*

* @param source

* Date String.

* @return string "YYYYMMDD" in the format of date strings.

*/

public static string Toplaindate (string source) {

Date date = Parsehyphendate (source);

SimpleDateFormat formatter = new SimpleDateFormat ("YyyyMMdd");

return Formatter.format (date);

}

/**

* Convert date strings in "YYYYMMDD" format to date strings in "YYYY-MM-DD" format.

*

* @param source

* Date String.

* @return string "YYYY-MM-DD" in the format of date strings.

*/

public static string Tohyphendate (string source) {

Date date = Parseplaindate (source);

SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");

return Formatter.format (date);

}

/**

* Gets the timestamp, converting the Date object to the timestamp type.

*

* @param date

* Date Object

* @return Timestamp time stamp

*/

public static Timestamp Gettimestamp (date date) {

return new Timestamp (Date.gettime ());

}

/**

* Gets the timestamp, converting the current date to a timestamp type.

*

* @return Timestamp time stamp

*/

public static Timestamp Gettimestamp () {

return new Timestamp (new Date (). GetTime ());

}

/**

* Convert date strings in "YYYYMMDD" format to objects of type timestamp.

*

* @param source

* Date String

* @return Timestamp time stamp

*/

public static Timestamp Parsetimestamp (String source) {

Date date = Parseplaindate (source);

return Gettimestamp (date);

}

/**

* Get an annual cycle

* Example:

* Xjpdateutil.getperiodyear ("20040229",-1);

* Xjpdateutil.getperiodyear ("20040228",-1);

* Xjpdateutil.getperiodyear ("20020230", 2);

*

* @param source

* Time string

* @param yearperiod

* Annual Cycle-1 represents the previous year of this time, by analogy.

* @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 Added

*

* @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 ());

}

/**

* Return milliseconds

*

* @param date

* Date

* @return return milliseconds

*/

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 Example: 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 months

* @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;

}

}

How Java Gets the timestamp

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.