This article describes the date tool class DateUtil in Android development. Share it for your reference, as follows:
/** * Date operation tool class. * @Project ERPForAndroid * @Package * @author chenlin * @version 1.0 */ @SuppressLint("SimpleDateFormat") public class DateUtil { private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); public static Date str2Date(String str) { return str2Date(str, null); } /** * String to time * @param str * @param format * @return */ public static Date str2Date(String str, String format) { if (str == null || () == 0) { return null; } if (format == null || () == 0) { format = FORMAT; } Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); date = (str); } catch (Exception e) { (); } 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 = (); (date); return c; } public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss return date2Str(c, null); } public static String date2Str(Calendar c, String format) { if (c == null) { return null; } return date2Str((), format); } public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss return date2Str(d, null); } public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss if (d == null) { return null; } if (format == null || () == 0) { format = FORMAT; } SimpleDateFormat sdf = new SimpleDateFormat(format); String s = (d); return s; } public static String getCurDateStr() { Calendar c = (); (new Date()); return () + "-" + (() + 1) + "-" + (Calendar.DAY_OF_MONTH) + "-" + (Calendar.HOUR_OF_DAY) + ":" + () + ":" + (); } /** * Get the string format of the current date * * @param format * @return */ public static String getCurDateStr(String format) { Calendar c = (); return date2Str(c, format); } /** * Format to seconds * * @param time * @return */ public static String getMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time); } /** * Format to day * * @param time * @return */ public static String getDay(long time) { return new SimpleDateFormat("yyyy-MM-dd").format(time); } /** * Format to milliseconds * * @param time * @return */ public static String getSMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time); } /** * Convert string to time format * * @param dateStr * string to convert * @param formatStr * The target string that requires formatting Example yyyy-MM-dd * @return Date Returns the converted time * @throws ParseException * Conversion exception */ public static Date StringToDate(String dateStr, String formatStr) { DateFormat sdf = new SimpleDateFormat(formatStr); Date date = null; try { date = (dateStr); } catch (ParseException e) { (); } return date; } /** * The interval between the conversion time input time and the current time * * @param timestamp * @return */ public static String converTime(long timestamp) { long currentSeconds = () / 1000; long timeGap = currentSeconds - timestamp;// The number of seconds difference from the current time String timeStr = null; if (timeGap > 24 * 60 * 60) {// More than 1 day timeStr = timeGap / (24 * 60 * 60) + "Day Ago"; } else if (timeGap > 60 * 60) {// 1 hour-24 hours timeStr = timeGap / (60 * 60) + "Hour ago"; } else if (timeGap > 60) {// 1 minute-59 minutes timeStr = timeGap / 60 + "Minutes ago"; } else {// 1 second - 59 seconds timeStr = "just"; } return timeStr; } /** * Convert strings to time format * * @param timestamp * @return */ public static String getStandardTime(long timestamp) { SimpleDateFormat sdf = new SimpleDateFormat("MM month dd day HH:mm"); Date date = new Date(timestamp * 1000); (date); return (date); } /** * Get the current date and time Date time format yyyy-MM-dd HH:mm:ss * * @return */ public static String currentDatetime() { return (now()); } /** * Format date and time Date and time format yyyy-MM-dd HH:mm:ss * * @return */ public static String formatDatetime(Date date) { return (date); } /** * Get the current time Time format HH:mm:ss * * @return */ public static String currentTime() { return (now()); } /** * Format time Time format HH:mm:ss * * @return */ public static String formatTime(Date date) { return (date); } /** * Get the <code></code> object of the current time * * @return */ public static Date now() { return new Date(); } public static Calendar calendar() { Calendar cal = (); (); return cal; } /** * Get the milliseconds of the current time * * See {@link System#currentTimeMillis()} for details * * @return */ public static long millis() { return (); } /** * * Get the current Chinese month * * @return */ public static int month() { return calendar().get() + 1; } /** * What day of the month you get * * @return */ public static int dayOfMonth() { return calendar().get(Calendar.DAY_OF_MONTH); } /** * What day of the week is today * * @return */ public static int dayOfWeek() { return calendar().get(Calendar.DAY_OF_WEEK); } /** * What day of the year is today * * @return */ public static int dayOfYear() { return calendar().get(Calendar.DAY_OF_YEAR); } /** * Determine whether the original date is before the target date * * @param src * @param dst * @return */ public static boolean isBefore(Date src, Date dst) { return (dst); } /** * Determine whether the original date is after the target date * * @param src * @param dst * @return */ public static boolean isAfter(Date src, Date dst) { return (dst); } /** * Determine whether the two dates are the same * * @param date1 * @param date2 * @return */ public static boolean isEqual(Date date1, Date date2) { return (date2) == 0; } /** * Determine whether a date is within a certain date range * * @param beginDate * Date range starts * @param endDate * End of date range * @param src * Date to be judged * @return */ public static boolean between(Date beginDate, Date endDate, Date src) { return (src) && (src); } /** * Get the last day of the current month * * HH:mm:ss is 0, milliseconds is 999 * * @return */ public static Date lastDayOfMonth() { Calendar cal = calendar(); (Calendar.DAY_OF_MONTH, 0); //M month set zero (Calendar.HOUR_OF_DAY, 0);// H sets zero (, 0);// m set zero (, 0);// s set zero (, 0);// S set zero (, () + 1);// Month +1 (, -1);// milliseconds-1 return (); } /** * Get the first day of the current month * * HH:mm:ss SS is zero * * @return */ public static Date firstDayOfMonth() { Calendar cal = calendar(); (Calendar.DAY_OF_MONTH, 1); //M month set 1 (Calendar.HOUR_OF_DAY, 0);// H sets zero (, 0);// m set zero (, 0);// s set zero (, 0);// S set zero return (); } private static Date weekDay(int week) { Calendar cal = calendar(); (Calendar.DAY_OF_WEEK, week); return (); } /** * Get Friday date * * Note: Calendar factory method {@link #calendar()} set the first day of each week to Monday, US, etc., the first day of each week to sunday * * @return */ public static Date friday() { return weekDay(); } /** * Get Saturday date * * Note: Calendar factory method {@link #calendar()} set the first day of each week to Monday, US, etc., the first day of each week to sunday * * @return */ public static Date saturday() { return weekDay(); } /** * Get Sunday date Note: Calendar factory method {@link #calendar()} Set the first day of each week to Monday, US, etc. The first day of each week to sunday * * @return */ public static Date sunday() { return weekDay(); } /** * Convert string date and time to type Date and time format yyyy-MM-dd HH:mm:ss * * @param datetime * @return */ public static Date parseDatetime(String datetime) throws ParseException { return (datetime); } /** * Convert string date to type Date time format yyyy-MM-dd * * @param date * @return * @throws ParseException */ public static Date parseDate(String date) throws ParseException { return (date); } /** * Convert string date to type Time format HH:mm:ss * * @param time * @return * @throws ParseException */ public static Date parseTime(String time) throws ParseException { return (time); } /** * Convert string date to type according to custom pattern * * @param datetime * @param pattern * @return * @throws ParseException */ public static Date parseDatetime(String datetime, String pattern) throws ParseException { SimpleDateFormat format = (SimpleDateFormat) (); (pattern); return (datetime); } /** * Format seconds into seconds * * @param second * @return */ public static String parseSecond(int second) { if (second >= 60) { return second / 60 + "point"; } else if (second >= 60 * 60) { return second / 60 * 60 + "hour"; } else if (second >= 60 * 60 * 24) { return second / 60 * 60 + "sky"; } else { return second + "Second"; } } /** * Compare time size * @param begin * @param end * @return */ public static int compareDate(String begin, String end) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm"); try { Date beginDate = (begin); Date endDate = (end); if (() < ()) { return 1; } else if (() > ()) { return -1; } else { return 0; } } catch (Exception exception) { (); } return 0; } /** * Year of obtaining * @param date * @return */ public int getYear(Date date){ Calendar c = (); (date); return (); } /** * Get Month * @param date * @return */ public int getMonth(Date date){ Calendar c = (); (date); return () + 1; } /** * Get the day of the week * @param date * @return */ public int getWeek(Date date){ Calendar c = (); (date); return (Calendar.WEEK_OF_YEAR); } /** * Date of acquisition * @param date * @return */ public int getDay(Date date){ Calendar c = (); (date); return (); } /** * Get the difference in days * @param begin * @param end * @return */ public long getDayDiff(Date begin, Date end){ long day = 1; if(() < ()){ day = -1; }else if(() == ()){ day = 1; }else { day += (() - ())/(24 * 60 * 60 * 1000) ; } return day; } }
PS: Here are a few online tools for your reference:
Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi
Online Perpetual Calendar:
http://tools./bianmin/wannianli
Online Lunar/Gregorian calendar conversion tool:
http://tools./bianmin/yinli2yangli
Unix timestamp conversion tool:
http://tools./code/unixtime
For more information about Android related content, please check out the topic of this site:Android date and time operation skills summary》、《Android development introduction and advanced tutorial》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.