Android Tool Class Dateutil

Source: Internet
Author: User
Tags date1

Some date-related functions that may be used in the development process are extracted as tool classes for your reference:


/** * Date manipulation tool class. * * @author Shimiso */public class Dateutil {private static final String FORMAT = "Yyyy-mm-dd HH:mm:ss";p ublic static Dat e str2date (String str) {return str2date (str, NULL);} public static Date str2date (String str, string format) {if (str = = NULL | | str.length () = = 0) {return null;} if (format = = NULL | | format.length () = = 0) {format = format;} Date date = null;try {SimpleDateFormat sdf = new SimpleDateFormat (format);d ate = Sdf.parse (str);} catch (Exception e) {E.P Rintstacktrace ();} return date;} public static Calendar Str2calendar (String str) {return Str2calendar (str, NULL);} public static Calendar Str2calendar (String str, string format) {Date date = str2date (str, format); if (Date = = null) {retur n NULL;} Calendar C = calendar.getinstance (); C.settime (date); return C;} public static String Date2str (Calendar c) {//Yyyy-mm-dd HH:mm:ssreturn date2str (c, null);} public static string Date2str (Calendar C, string format) {if (c = = null) {return null;} Return Date2str (C.gettime (), format);}public static String date2str (Date D) {//YYYY-MM-DD HH:mm:ssreturn date2str (d, null);} public static string Date2str (Date D, string format) {//Yyyy-mm-dd HH:mm:ssif (d = = null) {return null;} if (format = = NULL | | format.length () = = 0) {format = format;} SimpleDateFormat SDF = new SimpleDateFormat (format); String s = sdf.format (d); return s;} public static String Getcurdatestr () {Calendar c = calendar.getinstance (); C.settime (new Date ()); return C.get ( Calendar.year) + "-" + (C.get (calendar.month) + 1) + "-" + c.get (calendar.day_of_month) + "-" + c.get (calendar.hour_of_day) + ":" + c.get (Calendar.minute) + ":" + c.get (Calendar.second);} /** * Get the current date string format * * @param format * @return */public static string getcurdatestr (string format) {Calendar c = Calendar . getinstance (); return Date2str (c, format);} Format to seconds public static String Getmillon (long time) {return new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"). Format (time);} Format to day public static String GetDay (long time) {return new SimpleDateFormat ("yyyy-mm-dD "). Format (time);} Format to Ms public static String Getsmillon (long time) {return new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss-sss"). Format (Time );} /* Input is a string, format such as 20120102, to implement the function of the day, the returned format is string, such as 20120103 */public static string Stringdateplus (String row) throws Parseexception{string year=row.substring (0, 4); String month=row.substring (4,6); String day=row.substring (6); String date1=year+ "-" +month+ "-" +day; SimpleDateFormat sdf=new SimpleDateFormat ("YYYY-MM-DD");D ate Startdate=sdf.parse (date1);  Calendar cd = Calendar.getinstance (); Cd.settime (StartDate); Cd.add (calendar.date, 1); String datestr =sdf.format (Cd.gettime ()); String year1=datestr.substring (0,4); String month1=datestr.substring (5,7); String day1=datestr.substring (8); return year1+month1+day1;} /* Input is a string, format such as 20120102, to implement the function of reducing the day, the returned format is string, such as 20120101 */public static string Stringdatedecrease (String row) Throws Parseexception{string year=row.substring (0, 4); String month=row.substring (4,6); String day=row.substring (6); String date1=year+ "-" +montH + "-" +day; SimpleDateFormat sdf=new SimpleDateFormat ("YYYY-MM-DD");D ate Startdate=sdf.parse (date1);  Calendar cd = Calendar.getinstance (); Cd.settime (StartDate); Cd.add (Calendar.date,-1); String datestr =sdf.format (Cd.gettime ()); String year1=datestr.substring (0,4); String month1=datestr.substring (5,7); String day1=datestr.substring (8); return year1+month1+day1;} /* The format of the input is string, such as 20120101, and the format returned is string, such as 2012-01-01 */public static string Stringdatechange (string date) {if ( Date.length () = = "20120101". Length ()) {String year=date.substring (0, 4); String month=date.substring (4,6); String day=date.substring (6); return year+ "-" +month+ "-" +day;} Else{return date;}} /** * Date pushed backwards day * @param date format: 20120101 * @return 20120102 */public static string Tonextday (string date) {int year = Inte Ger.parseint (date.substring (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint ( Date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, day+1);D ate newdate = CalendaR.gettime (); SimpleDateFormat format = new SimpleDateFormat ("YyyyMMdd"); String da = Format.format (newdate); return da;} public static void Main (string[] args) {new Stringhandle (). Previousweekenddaybydate ("20130109");} /** * Gets the start date of the previous week of the current date (Sunday) */public static string previousweekbydate (String date) {int year = Integer.parseint (Date.substri Ng (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, day);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate);     int dayweek = Cal.get (Calendar.day_of_week);//Get the current date is the day of the one week if (1 = = DayWeek) {Cal.add (Calendar.day_of_month,-1); } cal.setfirstdayofweek (Calendar.sunday);//Set one the first day of the week, according to the Chinese habit one weeks the first day is Monday int s = cal.get (Calendar.day_of_week); /Get the current date is the day of the one week Cal.add (Calendar.date, Cal.getfirstdayofweek ()-s);//According to the rules of the calendar, the difference between the current date and the first day of the week of one weeks is reduced to cal.add (Calendar.week_of_year,-1);    String Imptimebegin = Sdf.format (Cal.gettime ());//System.out.println ("Date of week Sunday:" +imptimebegin); return imptimebegin;} /** * Gets the end date of the previous week of the current date (Saturday) */public static string previousweekenddaybydate (String date) {int year = Integer.parseint (date.s Ubstring (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, day);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate);     int dayweek = Cal.get (Calendar.day_of_week);//Get the current date is the day of the one week if (1 = = DayWeek) {Cal.add (Calendar.day_of_month,-1); } cal.setfirstdayofweek (Calendar.sunday);//Set one the first day of the week, according to the Chinese habit one weeks the first day is Monday int s = cal.get (Calendar.day_of_week);    /Get the current date is the day of the one week Cal.add (Calendar.date, Cal.getfirstdayofweek () + (6-s)); Cal.add (CALENDAR.WEek_of_year,-1);    String Imptimebegin = Sdf.format (Cal.gettime ());//System.out.println ("Saturday Date:" +imptimebegin); return imptimebegin;} /** * Gets the current day of the current week start date (Sunday) */public static string getcurrentweekfirstdaybydate (String date) {int year = Integer.parseint (d Ate.substring (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, day);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate);     int dayweek = Cal.get (Calendar.day_of_week);//Get the current date is the day of the one week if (1 = = DayWeek) {Cal.add (Calendar.day_of_month,-1); } cal.setfirstdayofweek (Calendar.sunday);//Set one the first day of the week, according to the Chinese habit one weeks the first day is Monday int s = cal.get (Calendar.day_of_week); /Get the current date is the day of the one week Cal.add (Calendar.date, Cal.getfirstdayofweek ()-s);//According to calendar rules, the difference between the current date and the first day of the week and one weeks is reduced to String imptime Begin = Sdf.formaT (Cal.gettime ());    System.out.println ("Date of week Sunday:" +imptimebegin); return imptimebegin;} /** * Gets the current date end date of the current week (Saturday) */public static string getcurrentweekenddaybydate (String date) {int year = Integer.parseint (dat E.substring (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, day);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate);     int dayweek = Cal.get (Calendar.day_of_week);//Get the current date is the day of the one week if (1 = = DayWeek) {Cal.add (Calendar.day_of_month,-1); } cal.setfirstdayofweek (Calendar.sunday);//Set one the first day of the week, according to the Chinese habit one weeks the first day is Monday int s = cal.get (Calendar.day_of_week);      /Get the current date is the day of the one week Cal.add (Calendar.date, Cal.getfirstdayofweek () + (6-s));    String Imptimebegin = Sdf.format (Cal.gettime ()); return imptimebegin;} /** * Return the first day of the last one months * @param date20120304 * @return 20120201 */public static string previousmonthbydate (String date) {//TODO auto-generated method Stubint Year = Integer.parseint (date.substring (0,4)); int month = Integer.parseint (date.substring (4,6)); int day = Integer.parseint ( Date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-2, 1);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate); String Imptimebegin = Sdf.format (Cal.gettime ());//System.out.println (Imptimebegin); return imptimebegin;} /** * Returns the first day of the next one months * @param date20120304 * @return 20120401 */public static string Nextmonthbydate (string date) {//TODO Au To-generated method Stubint year = Integer.parseint (date.substring (0,4)); int month = Integer.parseint (date.substring ( 4,6)); int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month, 1);D ate newdate = Calendar.gettime (); SimPledateformat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate); String Imptimebegin = Sdf.format (Cal.gettime ());//System.out.println (Imptimebegin); return imptimebegin;} /** * Returns the first day of the current month * @param date20120103 * @return 20120101 */public static string Getcurrentmonthfirstdaybydate (string date {int year = Integer.parseint (date.substring (0,4)), int month = Integer.parseint (date.substring (4,6)); Int day = Integer.parseint (date.substring (6)); Calendar calendar = Calendar.getinstance (); Calendar.set (year, month-1, 1);D ate newdate = Calendar.gettime ();     SimpleDateFormat sdf=new SimpleDateFormat ("YyyyMMdd");    Calendar cal = Calendar.getinstance ();    Cal.settime (newdate); String Imptimebegin = Sdf.format (Cal.gettime ()); return imptimebegin;}}


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.