Java date and time operations

Source: Internet
Author: User
Tags dateformat iso 8601 iso 8601 date iso 8601 date format julian day
Package COM. netcode. date; import Java. text. dateformat; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar; import Java. util. timezone;/*** convert date and string types */public class dateutil {/*** base ISO 8601 Date Format yyyymmdd I. E ., 20021225 for the 25th day of December in the year 2002 */public static final string iso_date_format = "yyyymmdd";/*** expanded ISO 8601 Date Format yyyy-mm-dd I. E ., 2002-12-25 for the 25th day of December in the year 2002 */public static final string iso_expanded_date_format = "yyyy-mm-dd";/*** yyyy-mm-dd hh: mm: SS */public static string datetime_pattern = "yyyy-mm-dd hh: mm: SS";/*** default lenient setting for getdate. */Private Static Boolean lenient_date = false;/*** temporarily do not use * @ Param JD * @ return */protected static final float normalizedjulian (float JD) {float F = math. round (JD + 0.5f)-0.5f; return F;}/*** convert a floating point value to a date format <br> * do not use * returns the date from a Julian. the Julian date will be converted to noon GMT, * such that it matches the nearest half-INTEGER (I. E ., A Julian Date of 1.4 gets * changed to 1.5, and 0.9 gets changed to 0.5 .) ** @ Param JD the Julian Date * @ return the Gregorian date */public static final date todate (float JD) {/* to convert a Julian day number to a Gregorian date, assume that it is for 0 hours and Greenwich time (so * that it ends in 0.5 ). do the following calculations, again dropping the fractional part of all * multiplicatons and divisions. note: This method will not give dates accurately on the * Gregorian prow.tic calendar, I. E ., the calendar you get by extending the Gregorian * Calendar backwards to years earlier than 1582. using the Gregorian leap year * rules. in particle, the method fails if y <400. */float z = (normalizedjulian (JD) + 0.5f; float W = (INT) (Z-1867216.25f)/36524.25f); float x = (INT) (W/4f); float a = z + 1 + w-X; float B = a + 1524; float c = (INT) (B-122.1)/365.25 ); float d = (INT) (365.25f * C); float E = (INT) (B-d)/30.6001); float F = (INT) (30.6001f * E ); int day = (INT) (B-d-f); int month = (INT) (e-1); If (month> 12) {month = month-12 ;} int year = (INT) (C-4715); // (if month is January or February) or C-4716 (otherwise) if (month> 2) {year --;} calendar c = calendar. getinstance (); C. set (calendar. year, year); C. set (calendar. month, month-1); // damn 0 offsets C. set (calendar. date, day); Return C. gettime ();}/*** returns the days between two dates. positive values indicate that * The second date is after the first, and negative values indicate, well, * the opposite. relying on specific times is problematic. ** @ Param early the "First Date" * @ Param late the "second date" * @ return the days between the two dates */public static final int daysbetween (date early, date late) {calendar C1 = calendar. getinstance (); Calendar C2 = calendar. getinstance (); c1.settime (early); c2.settime (late); Return daysbetween (C1, C2);}/*** returns the days between two dates. positive values indicate that * The second date is after the first, and negative values indicate, well, * the opposite. ** @ Param early * @ Param late * @ return the days between two dates. */public static final int daysbetween (calendar early, calendar late) {return (INT) (tojulian (late)-tojulian (early ));} /*** return a Julian Date based on the input parameter. this is * based from calculations found at * <a href = "http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html"> Julian day calculations * (Gregorian calendar) </a>, provided by Bill jeffrys. * @ Param c a calendar instance * @ return the Julian day number */public static final float tojulian (calendar c) {int y = C. get (calendar. year); int M = C. get (calendar. month); int d = C. get (calendar. date); int A = y/100; int B = A/4; int c = 2-A + B; float E = (INT) (365.25f * (Y + 4716); float F = (INT) (30.6001f * (m + 1); float JD = C + D + E + F-1524.5f; return JD;}/*** not required for the moment * return a Julian Date based on the input parameter. this is * based from calculations found at * <a href = "http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html"> Julian day calculations * (Gregorian calendar) </a>, provided by Bill jeffrys. * @ Param date * @ return the Julian day number */public static final float tojulian (date) {calendar c = calendar. getinstance (); C. settime (date); Return tojulian (c);}/*** date added * @ Param isostring date string * @ Param FMT Format * @ Param field year/month/day calendar ar. year/calendar. month/calendar. date * @ Param amount increase quantity * @ return * @ throws parseexception */public static final string dateincrease (string isostring, string FMT, int field, int amount) {try {calendar Cal = gregoriancalendar. getinstance (timezone. gettimezone ("GMT"); Cal. settime (stringtodate (isostring, FMT, true); Cal. add (field, amount); Return datetostring (Cal. gettime (), FMT);} catch (exception ex) {return NULL ;}/ *** time field rolling function. * rolls (up/down) a single unit of time on the given time field. ** @ Param isostring * @ Param field the time field. * @ Param Up indicates if rolling up or rolling down the field value. * @ Param expanded use formating Char's * @ exception parseexception if an unknown field value is given. */public static final string roll (string isostring, string FMT, int field, Boolean up) throws parseexception {calendar Cal = gregoriancalendar. getinstance (timezone. gettimezone ("GMT"); Cal. settime (stringtodate (isostring, FMT); Cal. roll (field, up); Return datetostring (Cal. gettime (), FMT);}/*** time field rolling function. * rolls (up/down) a single unit of time on the given time field. ** @ Param isostring * @ Param field the time field. * @ Param Up indicates if rolling up or rolling down the field value. * @ exception parseexception if an unknown field value is given. */public static final string roll (string isostring, int field, Boolean up) throws parseexception {return roll (isostring, datetime_pattern, field, up );} /*** convert string to date Java. util. date * @ Param datetext string * @ Param Format date format * @ Param lenient date out-of-bounds mark * @ return */public static date stringtodate (string datetext, string format, Boolean lenient) {If (datetext = NULL) {return NULL;} dateformat df = NULL; try {If (format = NULL) {df = new simpledateformat ();} else {df = new simpledateformat (format);} // setlenient avoids allowing dates like 9/32/2001 // which wowould otherwise parse to 10/2/2001 DF. setlenient (false); Return DF. parse (datetext);} catch (parseexception e) {return NULL ;}/ *** string to date Java. util. date * @ Param datetext string * @ Param Format date format * @ return */public static date stringtodate (string datestring, string format) {return stringtodate (datestring, format, lenient_date );} /*** convert string to date Java. util. date * @ Param datetext string */public static date stringtodate (string datestring) {return stringtodate (datestring, iso_expanded_date_format, lenient_date );} /** return a time string based on the time variable * @ return a time string * @ Param pattern time string style * @ Param date time variable */public static string datetostring (date, string Pattern) {If (date = NULL) {return NULL;} Try {simpledateformat sfdate = new simpledateformat (pattern); sfdate. setlenient (false); Return sfdate. format (date) ;}catch (exception e) {return NULL ;}} /*** return the time string yyyy-mm-dd * @ Param date * @ return */public static string datetostring (date) {return datetostring (date, iso_expanded_date_format);}/** returns the current time * @ return returns the current time */public static date getcurrentdatetime () {Java. util. calendar calnow = Java. util. calendar. getinstance (); Java. util. date dtnow = calnow. gettime (); Return dtnow;}/*** returns the current date string * @ Param pattern date string style * @ return */public static string getcurrentdatestring (string pattern) {return datetostring (getcurrentdatetime (), pattern);}/*** returns the current date string yyyy-mm-dd * @ return */public static string getcurrentdatestring () {return datetostring (getcurrentdatetime (), iso_expanded_date_format);}/*** returns the current date + time string yyyy-mm-dd hh: mm: SS * @ Param date * @ return */public static string datetostringwithtime (date) {return datetostring (date, datetime_pattern );} /*** date increase-add * @ Param date * @ Param days * @ return Java. util. date */public static date dateincreasebyday (date, int days) {calendar Cal = gregoriancalendar. getinstance (timezone. gettimezone ("GMT"); Cal. settime (date); Cal. add (calendar. date, days); Return Cal. gettime ();}/*** date increase-monthly increase * @ Param date * @ Param days * @ return Java. util. date */public static date dateincreasebymonth (date, int MNT) {calendar Cal = gregoriancalendar. getinstance (timezone. gettimezone ("GMT"); Cal. settime (date); Cal. add (calendar. month, MNT); Return Cal. gettime ();}/*** date increase-add by year * @ Param date * @ Param MNT * @ return Java. util. date */public static date dateincreasebyyear (date, int MNT) {calendar Cal = gregoriancalendar. getinstance (timezone. gettimezone ("GMT"); Cal. settime (date); Cal. add (calendar. year, MNT); Return Cal. gettime ();} /*** add date * @ Param date string yyyy-mm-dd * @ Param days * @ return date string yyyy-mm-dd */public static string dateincreasebyday (string date, int days) {return dateincreasebyday (date, iso_date_format, days );} /*** add date * @ Param date string * @ Param FMT date format * @ Param days * @ return */public static string dateincreasebyday (string date, string FMT, int days) {return dateincrease (date, FMT, calendar. date, days );} /*** convert the date string format ** @ Param SRC date string * @ Param srcfmt source date format * @ Param desfmt target date format * @ return */public static string stringtostring (string SRC, string srcfmt, string desfmt) {return datetostring (stringtodate (SRC, srcfmt), desfmt );}}

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.