Java tutorial
/** * Copyright 2008 - 2011 Simcore.org. * * 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.project.framework.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.StringUtils; /** * ? * <p> * ?Commons-Lang??. * * @author ray */ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { /** * ???? * * @param start * * @param end * * @return start<end0start=endstart>end */ public static int compare(Date start, Date end) { Calendar c1 = Calendar.getInstance(); c1.setTime(start); Calendar c2 = Calendar.getInstance(); c2.setTime(end); int year1 = c1.get(Calendar.YEAR); int year2 = c2.get(Calendar.YEAR); if (year1 != year2) { return year1 - year2; } int month1 = c1.get(Calendar.MONTH); int month2 = c2.get(Calendar.MONTH); if (month1 != month2) { return month1 - month2; } int day1 = c1.get(Calendar.DAY_OF_MONTH); int day2 = c2.get(Calendar.DAY_OF_MONTH); return day1 - day2; } /** * ? * @return */ public static Date getSystemTime() { return new Date(); } /** * , yyyy-MM-dd HH:mm:ss ?? * @param strDate * @param format ? * @return */ public static String dateStringFormat(Date date, String format) { if (date == null) return ""; SimpleDateFormat sdf = null; if (StringUtils.isNotBlank(format)) sdf = new SimpleDateFormat(format); else sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } /** * , yyyy-MM-dd HH:mm:ss ?? * @param strDate * @param format ? * @return */ public static String dateStringFormat(Date strDate) { if (strDate == null) return ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(strDate); } /** * , yyyyMMddHHmmss ?? * @param strDate * @param format ? * @return */ public static String dateStringFormatF(Date strDate) { if (strDate == null) return ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); return sdf.format(strDate); } /** * ,format null, yyyy-MM-dd HH:mm:ss ?? * @param strDate * @param format ? * @return */ public static Date stringDateFormat(String strDate, String format) { if (StringUtil.isNullOrEmpty(strDate)) { return null; } SimpleDateFormat sdf = null; if (format == null) sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); else sdf = new SimpleDateFormat(format); try { return sdf.parse(strDate); } catch (ParseException e) { return null; } } /** * ??. * * @param date ? * @return Date ?? */ public static Date getYesterday(Date date) { return addDays(date, -1); } /** * ??. * * @param date ? * @return Date ?? */ public static Date getTomorrow(Date date) { return addDays(date, 1); } /** * ??. * * @param date ? * @return Date ?? */ public static Date getBeforeYesterday(Date date) { return addDays(date, -2); } /** * ??. * * @param date ? * @return Date ?? */ public static Date getAfertTomorrow(Date date) { return addDays(date, 2); } /** * ??. * * @param date ? * @return Date ?? */ public static Date getMonthFistDay(Date date) { return setDays(date, 1); } /** * ?. * * @param date ? * @return Date ? */ public static Date getMonthLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int actualMaximum = calendar.getActualMaximum(Calendar.DATE); return setDays(date, actualMaximum); } /** * ??(?). * * @param date ? * @return Date ?? */ public static Date getBeforeTrad(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); do { calendar.roll(Calendar.DATE, false); } while (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7); return calendar.getTime(); } /** * ??(?) * * @param date ? * @return Date ?? */ public static Date getAfterTrad(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); do { calendar.roll(Calendar.DATE, true); } while (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7); return calendar.getTime(); } /** * ??(?) * * @param date ? * @return Date ?? */ public static Date getMonthFirstTrad(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getMonthFistDay(date)); while (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7) { calendar.roll(Calendar.DATE, true); } return calendar.getTime(); } /** * ??(?) * * @param date ? * @return Date ?? */ public static Date getMonthLastTrad(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getMonthLastDay(date)); while (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7) { calendar.roll(Calendar.DATE, false); } return calendar.getTime(); } /** * ? * @param start * @param end * @return */ public static long dateReduction(Date start, Date end) { long l = end.getTime() - start.getTime(); return l / 1000 / 60; } /** * ???? * @param date * @param second * @return date * @authorJiaYunqi */ public static Date getAddSecond(Date date, int second) { Calendar calendar = Calendar.getInstance(); if (date != null) { calendar.setTime(date); } calendar.add(Calendar.SECOND, second); return calendar.getTime(); } /** * ??? * @param time * @return */ public static Date getBeforeDate(int time) { Calendar c = Calendar.getInstance(); c.add(Calendar.MINUTE, (-1 * time)); return c.getTime(); } }