JAVA 时间 日期 相关 工具类
仅用于记录附源码如下
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static String DateToString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = format.format(date);
return timeStr;
}
public static String DateToString2(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String timeStr = format.format(date);
return timeStr;
}
public static String DateToString3(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String timeStr = format.format(date);
return timeStr;
}
public static String DateToString4(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateString = format.format(date);
String timeStr = dateString.replace("-", "").substring(2, 8);
return timeStr;
}
public static Date StringToDate(String string) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date StringToDate2(String string) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date date = null;
date = format.parse(string, pos);
return date;
}
public static Date GetDayAfterDays(Date date, int days) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
return now.getTime();
}
public static Date GetMouthAfterMouths(Date date, int mouths) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.add(Calendar.MONTH, mouths);
return now.getTime();
}
public static int GetDayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
public static int GetDiffTime(String time1, String time2) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat.parse(time1);
Date date2 = dateFormat.parse(time2);
long date1Time = date1.getTime();
long date2Time = date2.getTime();
int result = (int) (date2Time - date1Time);
return result;
}
public static int GetDiffDays(String time1, String time2) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(time1);
Date date2 = dateFormat.parse(time2);
long date1Time = date1.getTime();
long date2Time = date2.getTime();
int result = Math.abs((int) (date2Time - date1Time) / (1000 * 60 * 60 * 24));
return result;
}
public static Date MakeDateAll(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.HOUR, 23);
calendar.add(Calendar.MINUTE, 59);
calendar.add(Calendar.SECOND, 59);
Date date = calendar.getTime();
return date;
}
public static Boolean IsAlready(String time) {
String date = DateToString2(new Date());
Integer timeInt = Integer.parseInt(date.replaceAll("-", ""));
Integer timeInt1 = Integer.parseInt(time.replaceAll("-", ""));
if (timeInt <= timeInt1) {
return true;
} else {
return false;
}
}
}