Java methods for obtaining the current time

Source: Internet
Author: User
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 to get * Description: current time * copyright: Copyright 2010 * company: * @ author jiq * @ version 1.0 */public class xjpdateutil extends dateutil {public static final string [] months = {"January", "February", "March ", "April ", "August ",}; public static final string [] quarters = {"first quarter", "second quarter", "third quarter", "fourth quarter"}; Public xjpdateutil () {}/*** get the date string. ** <PRE> * Date string format: yyyymmdd * Where: * yyyy indicates 4-digit year. * Mm indicates a two-digit month. * Dd indicates a two-digit day. * </PRE> ** @ return string "yyyymmdd" format date string. */Public static string getdate () {simpledateformat formatter = new simpledateformat ("yyyymmdd"); Return formatter. format (new date ();}/*** get the string of the current year. ** <PRE> * Date string format: yyyy * Where: * yyyy indicates 4-digit year. * </PRE> ** @ return string "YYYY" string of the current year. */Public static string getnowyear () {simpledateformat formatter = new simpledateformat ("YYYY"); Return formatter. format (new date ();}/*** get the current monthly string. ** <PRE> * Date string format: Mm * Where: * Mm indicates 4-digit year. * </PRE> ** @ return string "YYYY" indicates the current monthly string. */Public static string getnowmonth () {simpledateformat formatter = new simpledateformat ("mm"); Return formatter. format (new date ();}/*** get the current monthly string. ** <PRE> * Date string format: dd * Where: * dd indicates 4-digit year. * </PRE> ** @ return string "YYYY" indicates the current monthly string. */Public static string getnowday () {simpledateformat formatter = new simpledateformat ("DD"); Return formatter. Format (new date ();}/*** get the date string. ** <PRE> * Date string format: yyyymmdd * Where: * yyyy indicates 4-digit year. * Mm indicates a two-digit month. * Dd indicates a two-digit day. * </PRE> ** @ Param date * indicates the date to be converted. * @ Return string "yyyymmdd" format date string. */Public static string getdate (date) {simpledateformat formatter = new simpledateformat ("yyyy-mm-dd"); Return formatter. format (date);}/*** get the date string. ** <PRE> * Date string format: yyyymmdd * Where: * yyyy indicates 4-digit year. * Mm indicates a two-digit month. * Dd indicates a two-digit day. * </PRE> ** @ Param date * indicates the date to be converted. * @ Return string "yyyymmdd" format date string. * // *** Convert 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 simpledateformat df = new simpledateformat ("yyyymmdd"); try {Java. util. date = DF. parse (datestr); return date;} catch (exception ex) {logger. write ("the date format is invalid or the date is blank! Please check! "); Return NULL;} // end try-catch} else if (xjptypechecker. isdatetime (datestr) {// datetime simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: Ss. SSS "); try {Java. util. date = DF. parse (datestr); return date;} catch (exception ex) {return NULL;} // end try-catch} // end if return NULL ;} /*** get the date string. ** <PRE> * Date string format: yyyy-mm-dd * Where: * yyyy indicates 4-digit year. * Mm indicates a two-digit month. * Dd indicates a two-digit day. * </PRE> ** @ return string "yyyy-mm-dd" format date string. */Public static string gethyphendate () {simpledateformat formatter = new simpledateformat ("yyyy-mm-dd"); Return formatter. format (new date ();}/*** get the date string. ** <PRE> * Date string format: yyyy-mm-dd * Where: * yyyy indicates 4-digit year. * Mm indicates a two-digit month. * Dd indicates a two-digit day. * </PRE> ** @ Param date * indicates the date to be converted. * @ Return string "yyyy-mm-dd" format date string. */Public static string gethyphendate (date) {simpledateformat formatter = new simpledateformat ("yyyy-mm-dd"); Return formatter. format (date);}/*** converts a date string in the "yyyymmdd" format 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);}/*** converts a date string in the format of "yyyy-mm-dd" 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);}/*** converts a date string in the "yyyy-mm-dd" format to a date string in the "yyyymmdd" format. ** @ Param source * Date string. * @ Return string "yyyymmdd" format date string. */Public static string toplaindate (string source) {date = parsehyphendate (source); simpledateformat formatter = new simpledateformat ("yyyymmdd"); Return formatter. format (date);}/*** converts a date string in the "yyyymmdd" format to a date string in the "yyyy-mm-dd" format. ** @ Param source * Date string. * @ Return string "yyyy-mm-dd" format date string. */Public static string tohyphendate (string source) {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) {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 ();}/*** converts a date string in the yyyymmdd format to an object of the timestamp type. ** @ Param source * Date string * @ return timestamp */public static timestamp parsetimestamp (string source) {date = parseplaindate (source); Return gettimestamp (date );} /*** obtain the annual cycle <br> * example: <br> * xjpdateutil. getperiodyear ("20040229",-1); <br> * xjpdateutil. getperiodyear ("20040228",-1); <br> * xjpdateutil. getperiodyear ("20020230", 2); <br> ** @ 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 = parseplaindate (newyear); string S = getdate (date); int ny = integer. parseint (s); int Sy = integer. parseint (newyear); While (ny> Sy) {Sy --; ny = integer. parseint (getdate (parsepla Indate (string. valueof (SY);} return string. valueof (SY);}/*** get the current date and time ** @ return string */public static string getcurrentdatestr () {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 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 * @ return returns millisecond */public static long getmillis (Java. util. date) {Java. util. calendar c = Java. util. calendar. getinstance (); C. settime (date); Return C. Gettimeinmillis ();}/*** get the current date and time * @ Param Format Date Format example: yyyy-mm-dd hh: mm * @ return string */public static string getnowdate (string format) {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 get1_cemondate (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 = 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 ;}}

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.