Java Time Processing Tool class--dateutils

Source: Internet
Author: User

Package com.hexiang.utils;/** * @ (#) Dateutil.java * * * @author Kidd * @version 1.00 2007/8/8 */import java.util.*;import Java.text.*;import Java.sql.timestamp;public class Dateutils {/** * time range: Years */public static final int year = 1;/** * Time range: Quarter degree */public static final int QUARTER = 2;/** * Time range: Month */public static final int month = 3;/** * Time range: */public static FINA l INT tendays = 4;/** * Time range: Week */public static final int WEEK = 5;/** * Time range: Day */public static final int daily = 6;/* base time * /private Date fiducialdate = null;private Calendar cal = null;private dateutils (Date fiducialdate) {if (Fiducialdate! = Nu ll) {this.fiducialdate = Fiducialdate;} else {this.fiducialdate = new Date (System.currenttimemillis ());} this.cal = Calendar.getinstance (); This.cal.setTime (this.fiducialdate); This.cal.set (calendar.hour_of_day, 0); This.cal.set (calendar.minute, 0); This.cal.set (Calendar.second, 0); This.cal.set (Calendar.millisecond, 0); This.fiducialdate = This.cal.getTime ();} /** * Get datehelper instance * * @param fIducialdate * Base time * @return Date */public static dateutils getinstance (date fiducialdate) {return new Dateutil S (fiducialdate);} /** * Gets the Datehelper instance, using the current time as the base time * * @return Date */public static dateutils getinstance () {return new dateutils (null);} /** * Gets the first day of the year * * @param offset * offset * @return Date */public date getfirstdayofyear (int offset) {Cal.settime ( This.fiducialdate); Cal.set (Calendar.year, Cal.get (calendar.year) + offset); Cal.set (Calendar.month, Calendar.january); Cal.set (Calendar.day_of_month, 1); return Cal.gettime ();} /** * Get the last day of the year * * @param offset * offset * @return Date */public date getlastdayofyear (int offset) {Cal.settime ( This.fiducialdate); Cal.set (Calendar.year, Cal.get (calendar.year) + offset); Cal.set (Calendar.month, Calendar.december); Cal.set (calendar.day_of_month); return Cal.gettime ();} /** * Get the first day of the quarter * * @param offset * offset * @return Date */public date getfirstdayofquarter (int offset) {Cal.sett IME (this.fiducialdate); CAL.add (Calendar.month, offset * 3); int mon = Cal.get (calendar.month); if (Mon >= calendar.january && Mon <= Ca Lendar. March) {Cal.set (calendar.month, calendar.january); Cal.set (Calendar.day_of_month, 1);} if (Mon >= calendar.april && Mon <= calendar.june) {cal.set (calendar.month, Calendar.april); Cal.set ( Calendar.day_of_month, 1);} if (Mon >= calendar.july && Mon <= calendar.september) {cal.set (calendar.month, calendar.july); Cal.set ( Calendar.day_of_month, 1);} if (Mon >= calendar.october && Mon <= calendar.december) {cal.set (calendar.month, Calendar.october); Cal.set (Calendar.day_of_month, 1);} return Cal.gettime ();} Public Date Getyesterday () {Long time = This.fiducialDate.getTime ()-* $ * * 1000;return new Date (time);} Public Date Gettomorrow () {Long time = This.fiducialDate.getTime () + * * * * * 1000;return new Date (time);} /** * Get the last day of the quarter * * @param offset * offset * @return Date */public date getlastdayofquarter (int oFfset) {cal.settime (this.fiducialdate); Cal.add (Calendar.month, offset * 3); int mon = Cal.get (calendar.month); if (Mon >= calendar.january && Mon <= calendar.march) {cal.set (calendar.month, Calendar.march); Cal.set ( Calendar.day_of_month, 31);} if (Mon >= calendar.april && Mon <= calendar.june) {cal.set (calendar.month, Calendar.june); Cal.set ( Calendar.day_of_month, 30);} if (Mon >= calendar.july && Mon <= calendar.september) {cal.set (calendar.month, Calendar.september); Cal.set (Calendar.day_of_month, 30);} if (Mon >= calendar.october && Mon <= calendar.december) {cal.set (calendar.month, Calendar.december); Cal.set (Calendar.day_of_month, 31);} return Cal.gettime ();} /** * Get the last day of the month * * @param offset * offset * @return Date */public date getfirstdayofmonth (int offset) {Cal.settim E (this.fiducialdate); Cal.add (calendar.month, offset); Cal.set (Calendar.day_of_month, 1); return Cal.gettime ();} /** * Get the last day of the month * * @param offset * offsets* @return Date */public date getlastdayofmonth (int offset) {cal.settime (this.fiducialdate); Cal.add (Calendar.month, Offset + 1); Cal.set (Calendar.day_of_month, 1); Cal.add (Calendar.day_of_month,-1); return Cal.gettime ();} /** * Get the first day of Lent * * @param offset * offset * @return Date */public date getfirstdayoftendays (int offset) {Cal.setti  Me (this.fiducialdate); int day = Cal.get (Calendar.day_of_month), if (day >=) {day = $;} else if (day >= one) {day = 11;} else {day = 1;} if (Offset > 0) {day = day + ten * Offset;int monoffset = Day/30;day = day% 30;cal.add (Calendar.month, monoffset); Cal . Set (Calendar.day_of_month, day);} else {int monoffset = ten * Offset/30;int dayoffset = ten * offset% 30;if ((day + Dayoffset) > 0) {day = day + dayoffs ET;} else {monoffset = Monoffset-1;day = day-dayoffset-10;} Cal.add (Calendar.month, Monoffset); Cal.set (Calendar.day_of_month, day);} return Cal.gettime ();} /** * Get the last day of Lent * * @param offset * offset * @return Date */public Date getlastdayoftendays (int offset) {Date date = Getfirstdayoftendays (offset + 1); Cal.settime (Date); Cal.add (Calendar.day_ Of_month,-1); return Cal.gettime ();} /** * Gets the first day of the week (MONDAY) * * @param offset * offset * @return Date */public date getfirstdayofweek (int offset) {cal. SetTime (this.fiducialdate); Cal.add (Calendar.day_of_month, offset * 7); Cal.set (Calendar.day_of_week, Calendar.monday); return Cal.gettime ();} /** * Gets the last day of the week (SUNDAY) * * @param offset * offset * @return Date */public date getlastdayofweek (int offset) {cal. SetTime (this.fiducialdate); Cal.add (Calendar.day_of_month, offset * 7); Cal.set (Calendar.day_of_week, Calendar.monday); Cal.add (Calendar.day_of_month, 6); return Cal.gettime ();} /** * Gets the first day of the specified time range * * @param daterangetype * Time range Type * @param offset * offset * @return Date */public Date getfirstdate (int daterangetype, int offset) {return null;} /** * Gets the last day of the specified time range * * @param daterangetype * Time range Type * @param offset * offset * @retUrn Date */public date getlastdate (int daterangetype, int offset) {return null;}  /** * The amount of time to add a specified calendar field to the base time according to the calendar rules * * @param field * Calendar field, using the Calendar field constants defined by the Calendar class * @param offset * Offset * @return Date */public date Add (int field, int offset) {cal.settime (this.fiducialdate); Cal.add (field, offset); return cal . GetTime ();} /** * Add a single time unit for the specified calendar field for the base time according to the calendar rules * * @param field * Calendar field, use Calendar field constants defined by the Calendar class * @param up * Specify calendar The scrolling direction of the value of the field. true: Scroll up/false: Scroll down * @return date */public date roll (int field, Boolean up) {cal.settime (this.fiducialdate); Cal.roll (f Ield, up); return Cal.gettime ();} /** * Convert String to date * * @param datestr * Date String * @param format * Date format * @return Date */public static Da Te strtodate (String datestr, string format) {Date date = Null;if (datestr! = null && (!datestr.equals (""))) {Datef Ormat df = new SimpleDateFormat (format); try {date = Df.parse (DATESTR);} catch (ParseException e) {e.printstacktrace ();}} return date;} /* * Convert string to date, date format yyyy-mm-dd hh:ss * * @param datestr * Date String * @return date */public static Date Strtodate (String datestr) {Date date = Null;if (datestr! = null && (!datestr.equals ("))) {if (Datestr.matches (" \\d{4}-\\d{1,2}-\\d{1,2} ") {datestr = datestr + "00:00";} else if (Datestr.matches ("\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}")) {datestr = Datestr + ": xx";} else {System.out.println ( Datestr + "Malformed"); return null;} DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd hh:ss"); try {date = Df.parse (DATESTR);} catch (ParseException e) {E.print StackTrace ();}} return date;}  /** * Convert Date to String * * @param date * Date instance * @param format * Date format * @return Date */public static string DATETOSTR (date date, String format) {return (date = = null)? "": New SimpleDateFormat (format). format (date);} public static String datetostr (date date) {return (date = = null)? "": New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (date); /** * Get current date year-month-day * * @return Date */publicStatic String Getcurrentdate () {DateFormat f = new SimpleDateFormat ("Yyyy-mm-dd"); return F.format (calendar.getinstance (). GetTime ());} public static void Main (string[] args) {dateutils datehelper = dateutils.getinstance ();/* year */for (int i =-5; I <= 5; i++) {System.out.println ("firstdayofyear (" + i + ") =" + Datehelper.getfirstdayofyear (i)); System.out.println ("lastdayofyear (" + i + ") =" + Datehelper.getlastdayofyear (i));} /* Quarter */for (int i =-5; I <= 5; i++) {System.out.println ("firstdayofquarter (" + i + ") =" + DATEHELPER.GETFIRSTDA Yofquarter (i)); System.out.println ("Lastdayofquarter (" + i + ") =" + Datehelper.getlastdayofquarter (i));} /* Month */for (int i =-5; I <= 5; i++) {System.out.println ("firstdayofmonth (" + i + ") =" + DATEHELPER.GETFIRSTDAYOFM Onth (i)); System.out.println ("Lastdayofmonth (" + i + ") =" + Datehelper.getlastdayofmonth (i));} /* Week */for (int i =-5; I <= 5; i++) {System.out.println ("firstdayofweek (" + i + ") =" + Datehelper.getfirstdayofwee K (i)); System.out.println ("Lastdayofweek (" + i + ") =" + Datehelper.getlastdayofweek (i));} /* tendays */for (int i =-5; I <= 5; i++) {System.out.println ("firstdayoftendays (" + i + ") =" + DATEHELPER.GETFIRSTDA Yoftendays (i)); System.out.println ("lastdayoftendays (" + i + ") =" + datehelper.getlastdayoftendays (i));}} /** * takes the current date as a string, "xxxx year XX month XX Day" * * @return java.lang.String */public static string Getprintdate () {String printdate = "" ; Calendar calendar = Calendar.getinstance (); Calendar.settime (new Date ());p rintdate + = Calendar.get (calendar.year) + "year" ;p rintdate + = (Calendar.get (calendar.month) + 1) + "month";p rintdate + = Calendar.get (calendar.date) + "Day"; return printdate;}  /** * Converts the specified date string to a Date object * * @param datestr * Date String * @return java.util.Date */public static Date GetDate (string DATESTR, String format) {if (datestr = = null) {return new Date ();} if (format = = null) {format = "yyyy-mm-dd";} SimpleDateFormat SDF = new SimpleDateFormat (format); try {Date date = Sdf.parse (DATESTR); returnDate;} catch (Exception e) {return null;}} /** * Gets the string form date of the corresponding date from the specified timestamp "xxxx-xx-xx" * @param dateTime * @return, string */public static string GETDATEFROMD Atetime (Timestamp datetime) {simpledateformat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); return Sdf.format (DateTime). ToString ();} /** * Get current time return java.sql.Timestamp * * @return Timestamp */public static Timestamp Getnowtimestamp () {Long curtime = System.currenttimemillis (); return new Timestamp (curtime);}

  

Java Time Processing Tool class--dateutils

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.