- String and Date conversion
(1) is implemented based on SimpleDateFormat:package com.bky.df; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class DateLangUtils{ public static void main(String[] args) throws ParseException { Date nowDate = new Date(); String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = "2018-12-02 16:33:15"; System.out.println(getString(nowDate, formatString)); System.out.println(getDate(dateString, formatString)); System.out.println(getDatePos(dateString, formatString)); } /** * Format Date using SimpleDateFormat * * @param date * @param formatString * @return */ public static String getString(Date date, String formatString) { SimpleDateFormat formatter = new SimpleDateFormat(formatString); return formatter.format(date); } /** * Use SimpleDateFormat to parse date strings * * @param dateString * @param formatString * @return * @throws ParseException */ public static Date getDate(String dateString, String formatString) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(formatString); return formatter.parse(dateString); } /** * Use SimpleDateFormat to parse date strings * * @param dateString * @param formatString * @return */ public static Date getDatePos(String dateString, String formatString) { SimpleDateFormat formatter = new SimpleDateFormat(formatString); return formatter.parse(dateString, new ParsePosition(0)); } }
(2) Implemented using commons components
import java.text.ParseException; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; public static void main(String[] args) throws ParseException { Date nowDate = new Date(); String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = "2018-12-02 16:33:15"; System.out.println(DateFormatUtils.format(nowDate, formatString)); System.out.println(DateUtils.parseDate(dateString, formatString)); }
- Date-related operations are
implemented based on Calendar
Calendar cal = Calendar.getInstance(); // Set Calendar time(1.Date 2.longofmills) cal.setTime( new Date()); cal.setTimeInMillis(System.currentTimeMillis()); // Set the year, month, day, hour, minute, second, millisecond cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)); // Calendar.MONTH starts from 0 to indicate January, and needs to be converted to display/set cal.set( Calendar.MONTH, cal.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)); // 24小时制 cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)); // 12小时制 cal.set(Calendar.HOUR, cal.get(Calendar.HOUR)); cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)); cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND)); // Set the parameters related to the week // Set the day of the week (1 is Sunday, 7 is Saturday) cal.set(Calendar.DAY_OF_WEEK, cal.get(Calendar.DAY_OF_WEEK)); // Set the week of the month (The default area is calculated from Sunday, and the week is calculated by the calendar) cal.set(Calendar.WEEK_OF_MONTH, cal.get(Calendar.WEEK_OF_MONTH)); // Set the week of the current month (the setting is calculated by the number of days in the current month) Weeks, 7 days a week) cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // Set the first week of the year cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR )); // Set the first day of the month cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); // Set the last day of the month cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0);
Date related operations in java
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324648386&siteId=291194637
Ranking