DateTime日期操作
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 static java.lang.Long.*;
public class DateTime {
public static String getDateTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
public static String getDateTimeExt() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return df.format(new Date());
}
public static String getDateTimeExts() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return df.format(new Date());
}
public static String getDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(new Date());
}
public static String getDateExt() {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
return df.format(new Date());
}
public static String getWeek() {
Calendar cd = Calendar.getInstance();
return String.valueOf(cd.get(Calendar.WEEK_OF_YEAR));
}
public static String getYear() {
Calendar cd = Calendar.getInstance();
return String.valueOf(cd.get(Calendar.YEAR));
}
public static String getMonth() {
Calendar cd = Calendar.getInstance();
return String.valueOf(cd.get(Calendar.MONTH) + 1);
}
public static String getDay() {
Calendar cd = Calendar.getInstance();
return String.valueOf(cd.get(Calendar.DAY_OF_MONTH));
}
public static long differenceDay(String strDateTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null, date = null;
try {
now = df.parse(getDateTime());
date = df.parse(strDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
return l / (24 * 60 * 60 * 1000);
}
public static String[] ergodicDate(int st_year, int st_month, int st_day, int end_year, int end_month,
int end_day) {
StringBuilder sb = new StringBuilder();
Calendar start = Calendar.getInstance();
st_month = st_month - 1;
start.set(st_year, st_month, st_day);
Long startTIme = start.getTimeInMillis();
Calendar end = Calendar.getInstance();
end_month = end_month - 1;
end.set(end_year, end_month, end_day);
Long endTime = valueOf(end.getTimeInMillis());
Long oneDay = valueOf(1000 * 60 * 60 * 24L);
Long time = startTIme;
while (time <= endTime) {
Date d = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
sb.append(df.format(d)).append(",");
time += oneDay;
}
return sb.toString().split(",");
}
public static long differenceHour(String strDateTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null, date = null;
try {
now = df.parse(getDateTime());
date = df.parse(strDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
return l / (60 * 60 * 1000);
}
public static long differenceMin(String strDateTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null, date = null;
try {
now = df.parse(getDateTime());
date = df.parse(strDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
return l / (60 * 1000);
}
public static long differenceS(String strDateTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null, date = null;
try {
now = df.parse(getDateTime());
date = df.parse(strDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
return l / 1000;
}
public static ArrayList<String> getMonthBetween(String minDate, String maxDate) {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
min.setTime(sdf.parse(minDate));
max.setTime(sdf.parse(maxDate));
} catch (ParseException e) {
e.printStackTrace();
}
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
while (min.before(max)) {
result.add(sdf.format(min.getTime()));
min.add(Calendar.MONTH, 1);
}
return result;
}
}