Java tutorial
/** * Copyright 2018 ? http://www.renren.io * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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 io.renren.common.utils; import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.text.SimpleDateFormat; import java.util.Date; /** * ? * * @author chenshun * @email sunlightcs@gmail.com * @date 20161221 ?12:53:33 */ public class DateUtils { /** ?(yyyy-MM-dd) */ public final static String DATE_PATTERN = "yyyy-MM-dd"; /** ?(yyyy-MM-dd HH:mm:ss) */ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; /** * ? ?yyyy-MM-dd * @param date * @return yyyy-MM-dd? */ public static String format(Date date) { return format(date, DATE_PATTERN); } /** * ? ?yyyy-MM-dd * @param date * @param pattern ?DateUtils.DATE_TIME_PATTERN * @return yyyy-MM-dd? */ public static String format(Date date, String pattern) { if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; } /** * ?? * @param strDate * @param pattern ?DateUtils.DATE_TIME_PATTERN */ public static Date stringToDate(String strDate, String pattern) { if (StringUtils.isBlank(strDate)) { return null; } DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); return fmt.parseLocalDateTime(strDate).toDate(); } /** * ???? * @param week 0-1-212 * @return date[0]?date[1]? */ public static Date[] getWeekStartAndEnd(int week) { DateTime dateTime = new DateTime(); LocalDate date = new LocalDate(dateTime.plusWeeks(week)); date = date.dayOfWeek().withMinimumValue(); Date beginDate = date.toDate(); Date endDate = date.plusDays(6).toDate(); return new Date[] { beginDate, endDate }; } /** * ?/? * * @param date * @param seconds ? * @return /?? */ public static Date addDateSeconds(Date date, int seconds) { DateTime dateTime = new DateTime(date); return dateTime.plusSeconds(seconds).toDate(); } /** * ?/? * * @param date * @param minutes ? * @return /?? */ public static Date addDateMinutes(Date date, int minutes) { DateTime dateTime = new DateTime(date); return dateTime.plusMinutes(minutes).toDate(); } /** * ??/? * * @param date * @param hours ?? * @return /??? */ public static Date addDateHours(Date date, int hours) { DateTime dateTime = new DateTime(date); return dateTime.plusHours(hours).toDate(); } /** * ?/? * * @param date * @param days ? * @return /?? */ public static Date addDateDays(Date date, int days) { DateTime dateTime = new DateTime(date); return dateTime.plusDays(days).toDate(); } /** * ?/? * * @param date * @param weeks ? * @return /?? */ public static Date addDateWeeks(Date date, int weeks) { DateTime dateTime = new DateTime(date); return dateTime.plusWeeks(weeks).toDate(); } /** * ?/? * * @param date * @param months ? * @return /?? */ public static Date addDateMonths(Date date, int months) { DateTime dateTime = new DateTime(date); return dateTime.plusMonths(months).toDate(); } /** * ?/? * * @param date * @param years ? * @return /?? */ public static Date addDateYears(Date date, int years) { DateTime dateTime = new DateTime(date); return dateTime.plusYears(years).toDate(); } }