时间处理方法集合 java
package com.XXX.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MyDateUtil {
private final static String DATE_FORMAT_YYYY = "yyyy";
private final static String DATE_FORMAT_MM = "MM";
private final static String DATE_FORMAT_dd = "dd";
private final static String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private final static String DATE_FORMAT_YYMMDDHHMMSS = "yyMMddHHmmss";
private final static String DATE_FORMAT_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
public static String getLastYear() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY);
return String.valueOf(Integer.valueOf(sdf.format(new Date()))-1);
}
public static String getNowYear() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY);
return sdf.format(new Date());
}
public static String getNowMonth() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_MM);
return sdf.format(new Date());
}
public static String getNowDay() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_dd);
return sdf.format(new Date());
}
public static String getNowDate() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
return sdf.format(new Date());
}
public static String getYearToSecondString() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYMMDDHHMMSS);
return sdf.format(new Date());
}
public static String getYearToMillisecondString() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHMMSSSSS);
return sdf.format(new Date());
}
public static String getConvertDatesFormat(String dateData) {
String newDateData = "";
if(dateData!=null && !"".equals(dateData.trim())){
String dateArray[] = dateData.split(" ")[0].split("/");
String timeArray[] = dateData.split(" ")[1].split(":");
String yearString = dateArray[0].trim();
String monthString = dateArray[1].trim();
String dayString = dateArray[2].trim();
String hourString = timeArray[0].trim();
String minuteString = timeArray[1].trim();
String secondString = timeArray[2].trim();
monthString = monthString.length()<2?("0"+monthString):monthString;
dayString = dayString.length()<2?("0"+dayString):dayString;
hourString = hourString.length()<2?("0"+hourString):hourString;
minuteString = minuteString.length()<2?("0"+minuteString):minuteString;
secondString = secondString.length()<2?("0"+secondString):secondString;
newDateData = yearString+"-"+monthString+"-"+dayString+" "+hourString+":"+minuteString+":"+secondString;
}
return newDateData;
}
public static String getConvertDatesFormat2(String dateData) {
String dateStr = "";
if(dateData!=null && dateData.indexOf("T")>=0 && dateData.indexOf("+")>=0) {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = df.parse(dateData);
SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
Date date1 = df1.parse(date.toString());
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateStr = df2.format(date1);
} catch (ParseException e) {
e.printStackTrace();
}
}
return dateStr;
}
public static String getLastOrNextDays(int index) {
String dateString = "";
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + index);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
dateString = format.format(today);
return dateString;
}
public static List<List<String>> getDateList(int days) {
List<List<String>> dateList = new ArrayList<List<String>>();
for(int i=days;i<0;i++){
List<String> dateListItem = new ArrayList<String>();
String dateBegin = MyDateUtil.getLastOrNextDays(i);
String dateEnd = MyDateUtil.getLastOrNextDays(i+1);
dateListItem.add(dateBegin);
dateListItem.add(dateEnd);
dateList.add(dateListItem);
}
return dateList;
}
public static List<String> getDateSegment(String startTime, String endTime) {
List<String> days = new ArrayList<String>();
try {
if(startTime.equals(endTime)) {
days.add(startTime);
}else {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static String getDateByBeforeOrAfter(String dateStr, int num){
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day1 = c.get(Calendar.DATE);
c.set(Calendar.DATE, day1 + num);
String newDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return newDate;
}
public static String getTimeByMinute(String oldTime, int minute) {
String newTime = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
try {
if(oldTime==null || "".equals(oldTime) || oldTime.length()<10) {
return newTime;
}
if(oldTime.length()==10) {
if(minute>=0) {
oldTime = oldTime + " 00:00:00";
}else {
oldTime = oldTime + " 23:59:59";
}
}
Date date = sdf.parse(oldTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minute);
newTime = sdf.format(calendar.getTime());
} catch (ParseException e) {
newTime = "";
}
return newTime;
}
public static String getTimeDifference(String minuendTime, String reductionTime) {
String timeDifference = "";
try {
if(minuendTime!=null && !"".equals(minuendTime) && minuendTime.length()==19
&& reductionTime!=null && !"".equals(reductionTime) && reductionTime.length()==19) {
minuendTime = minuendTime.replaceAll("/", "-");
reductionTime = reductionTime.replaceAll("/", "-");
SimpleDateFormat simpleFormat = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
Date minuendDate = simpleFormat.parse(minuendTime);
Date reductionDate = simpleFormat.parse(reductionTime);
long minuend = minuendDate.getTime();
long reduction = reductionDate.getTime();
int minutes = (int) ((minuend - reduction) / (1000 * 60));
timeDifference = String.valueOf(minutes);
}
} catch (ParseException e) {
timeDifference = "";
}
return timeDifference;
}
public static String getTimeDifferenceForByDay(String minuendTime, String reductionTime) {
String timeDifference = "";
try {
if(minuendTime!=null && !"".equals(minuendTime)
&& reductionTime!=null && !"".equals(reductionTime)) {
minuendTime = minuendTime.length()==10?(minuendTime + " 00:00:00"):minuendTime;
reductionTime = reductionTime.length()==10?(reductionTime + " 00:00:00"):reductionTime;
minuendTime = minuendTime.replaceAll("/", "-");
reductionTime = reductionTime.replaceAll("/", "-");
SimpleDateFormat simpleFormat = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
Date minuendDate = simpleFormat.parse(minuendTime);
Date reductionDate = simpleFormat.parse(reductionTime);
long minuend = minuendDate.getTime();
long reduction = reductionDate.getTime();
int day = (int) ((minuend - reduction) / (1000 * 60 * 60 * 24));
timeDifference = String.valueOf(day);
}
} catch (ParseException e) {
timeDifference = "";
}
return timeDifference;
}
public static void main(String[] args) {
try {
System.out.println(getTimeByMinute("2019-04-05 00:50:00", 420-480));
} catch (Exception e) {
e.printStackTrace();
}
}
}