Java tutorial
/** * Copyright 2014-2016 whatlookingfor@gmail.com(Jonathan) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.whatlookingfor.common.utils; import org.apache.commons.lang3.time.DateFormatUtils; import java.text.ParseException; import java.util.Calendar; import java.util.Date; /** * ? * * @author Jonathan * @version 2016/6/27 15:04 * @since JDK 7.0+ */ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" }; /** * ?yyyy-MM-dd) * * @param date * @param pattern ? * @return ? */ public static String format(Date date, Object... pattern) { if (date == null) { return null; } String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * ?,??yyyy-MM-dd HH:mm:ss * * @param date * @return , ?yyyy-MM-dd HH:mm:ss */ public static String getDateTime(Date date) { return format(date, "yyyy-MM-dd HH:mm:ss"); } /** * ??,??yyyy-MM-dd HH:mm:ss * * @return , ?yyyy-MM-dd HH:mm:ss */ public static String getDateTime() { return getDateTime(new Date()); } /** * ? ?yyyy-MM-dd pattern?"yyyy-MM-dd" "HH:mm:ss" "E" * * @param pattern ? * @return ? */ public static String getDate(String pattern) { return format(new Date(), pattern); } /** * ? ?yyyy-MM-dd * * @return yyyy-MM-dd */ public static String getDate() { return getDate("yyyy-MM-dd"); } /** * ??(HH:mm:ss) * * @return HH:mm:ss? */ public static String getTime() { return format(new Date(), "HH:mm:ss"); } /** * ?? * * @return yyyy? */ public static String getYear() { return format(new Date(), "yyyy"); } /** * ?? * * @return MM? */ public static String getMonth() { return format(new Date(), "MM"); } /** * ?dd * * @return dd? */ public static String getDay() { return format(new Date(), "dd"); } /** * ? ?E * * @return ? */ public static String getWeek() { return format(new Date(), "E"); } /** * ?{@value parsePatterns} * <p> * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" } * * @param str * @return */ public static Date parseDate(Object str) { if (str == null) { return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * ? * * @return */ public static long pastDays(Date before, Date after) { if (after == null) { after = new Date(); } long milliseconds = after.getTime() - before.getTime(); return milliseconds / (1000 * 60 * 60 * 24); } public static long pastHour(Date before, Date after) { if (after == null) { after = new Date(); } long milliseconds = after.getTime() - before.getTime(); return milliseconds / (1000 * 60 * 60); } public static long pastMinutes(Date before, Date after) { if (after == null) { after = new Date(); } long milliseconds = after.getTime() - before.getTime(); return milliseconds / (1000 * 60); } /** * ?,::. * * @param date * @return , ::. */ public static String formatDateTime(Date date) { long timeMillis = date.getTime(); long day = timeMillis / (24 * 60 * 60 * 1000); long hour = (timeMillis / (60 * 60 * 1000) - day * 24); long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss; } /** * ?,::. * @param timeMillis * @return */ public static String formatDateTime(long timeMillis) { long day = timeMillis / (24 * 60 * 60 * 1000); long hour = (timeMillis / (60 * 60 * 1000) - day * 24); long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss; } /** * * * @param date * @param field ,?Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR * @param amount * @return ? */ public static Date addDate(Date date, int field, int amount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(field, amount); return calendar.getTime(); } }