Android tool-type DateUtil

Source: Internet
Author: User
Tags date1

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


/*** Date operation tool class. ** @ author shimiso */public class DateUtil {private static final String FORMAT = "yyyy-MM-dd HH: mm: ss"; public static Date 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); date = sdf. parse (str);} catch (Exception e) {e. printStackTrace ();} 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) {return null;} Calendar c = Calendar ar. 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 Ar c = Calendar ar. 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 String format of the current date ** @ 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) ;}// the format is public static String getDay (long time) {return new SimpleDateFormat ("yyyy-MM-dd "). format (time);} // format to milliseconds public static String getSMillon (long time) {return new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss-SSS "). format (time);}/* The input format is String, such as 20120102, to add a 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 "); date startDate = sdf. parse (date1); Calendar cd = Calendar ar. 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 input format is String, for example, 20120102. The return format is String, for example 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 "); date startDate = sdf. parse (date1); Calendar cd = Calendar ar. 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 input format is String, for example, 20120101. The returned format is String, for example, 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 ;}} /*** push the date one day backward * @ param date Format: 20120101 * @ return 20120102 */public static String tonextday (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, day + 1); Date newdate = calendar. getTime (); SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd"); String da = format. format (newdate); return da;} public static void main (String [] args) {new StringHandle (). previusweekenddaybydate ("20130109");}/*** get the start date of the previous week (Sunday) */public static String previusweekbydate (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, day); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. getInstance (); cal. setTime (newdate); int dayWeek = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date if (1 = dayWeek) {cal. add (Calendar. DAY_OF_MONTH,-1);} cal. setFirstDayOfWeek (Calendar. SUNDAY); // set the first day of a week. According to China's habits, the first day of a week is Monday int s = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date. cal. add (Calendar. DATE, cal. getFirstDayOfWeek ()-s); // based on the calendar rule, subtract the current date from the day of the week to the day of the week (cal. add (Calendar. WEEK_OF_YEAR,-1); String imptimeBegin = sdf. format (cal. getTime (); // System. out. println ("date of week and Sunday:" + imptimeBegin); return imptimeBegin;}/*** get the end date of the previous week of the current date (Saturday) */public static String previousWeekEndDayByDate (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, day); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. getInstance (); cal. setTime (newdate); int dayWeek = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date if (1 = dayWeek) {cal. add (Calendar. DAY_OF_MONTH,-1);} cal. setFirstDayOfWeek (Calendar. SUNDAY); // set the first day of a week. According to China's habits, the first day of a week is Monday int s = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date. 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;}/*** get the start date of the current week (Sunday) */public static String getCurrentWeekFirstDayByDate (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, day); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. getInstance (); cal. setTime (newdate); int dayWeek = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date if (1 = dayWeek) {cal. add (Calendar. DAY_OF_MONTH,-1);} cal. setFirstDayOfWeek (Calendar. SUNDAY); // set the first day of a week. According to China's habits, the first day of a week is Monday int s = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date. cal. add (Calendar. DATE, cal. getFirstDayOfWeek ()-s); // according to the calendar rule, the difference between the current date minus the day of the week and the first day of the week String imptimeBegin = sdf. format (cal. getTime (); // System. out. println ("date of week and Sunday:" + imptimeBegin); return imptimeBegin;}/*** get the end date of the current week (Saturday) */public static String getCurrentWeekEndDayByDate (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, day); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. getInstance (); cal. setTime (newdate); int dayWeek = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date if (1 = dayWeek) {cal. add (Calendar. DAY_OF_MONTH,-1);} cal. setFirstDayOfWeek (Calendar. SUNDAY); // set the first day of a week. According to China's habits, the first day of a week is Monday int s = cal. get (Calendar. DAY_OF_WEEK); // obtain the day of the week from the current date. cal. add (Calendar. DATE, cal. getFirstDayOfWeek () + (6-s); String imptimeBegin = sdf. format (cal. getTime (); return imptimeBegin;}/*** returns the first day of the previous month * @ param date20120304 * @ return 20120201 */public static String previusmonthbydate (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); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. getInstance (); cal. setTime (newdate); String imptimeBegin = sdf. format (cal. getTime (); // System. out. println (imptimeBegin); return imptimeBegin;}/*** returns the first day of the next month * @ param date20120304 * @ return 20120401 */public static String nextMonthByDate (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, 1); Date newdate = calendar ar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. 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); Date newdate = calendar. getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar cal = Calendar ar. 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.