public class TimeUtils {
public static final String HOUR_FORMAT = "HH:mm";
public static final String YEAR_FORMAT = "yyyy-MM-dd";
public static String milsToDate() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm");
String time = format.format(date);
return time;
}
public static int getDateSx() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
int hour = cal.get(Calendar.HOUR_OF_DAY);
int val=0;
if (hour >= 6 && hour < 8) {
val=0;
// "早上好"
} else if (hour >= 8 && hour < 11) {
val=0;
// 上午好
} else if (hour >= 11 && hour < 13) {
val=1;// 中午好
} else if (hour >= 13 && hour < 18) {
val= 2;// 下午好
} else {
val= 3;// 晚上好
}
return val;
}
public static String commonDateTimeFormat(Date date) {
SimpleDateFormat format = new SimpleDateFormat(HOUR_FORMAT);
String str = format.format(date);
return str;
}
public static Date commonDateTimeParse(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat(HOUR_FORMAT);
try {
Date date = format.parse(dateStr);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYear(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(YEAR_FORMAT);
String format = dateFormat.format(date);
return format;
}
// 获取当前时间yyyy-MM-dd HH:mm
public static String getNowTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str = dateFormat.format(now);
Calendar calendar = Calendar.getInstance();
return str;
}
// 获取当前时间推前一个月的时间 yyyy-MM-dd HH:mm
public static String getUpMothDate() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, -1);// 减一个月
str = sdf.format(lastDate.getTime());
return str;
}
}
public static final String HOUR_FORMAT = "HH:mm";
public static final String YEAR_FORMAT = "yyyy-MM-dd";
public static String milsToDate() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm");
String time = format.format(date);
return time;
}
public static int getDateSx() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
int hour = cal.get(Calendar.HOUR_OF_DAY);
int val=0;
if (hour >= 6 && hour < 8) {
val=0;
// "早上好"
} else if (hour >= 8 && hour < 11) {
val=0;
// 上午好
} else if (hour >= 11 && hour < 13) {
val=1;// 中午好
} else if (hour >= 13 && hour < 18) {
val= 2;// 下午好
} else {
val= 3;// 晚上好
}
return val;
}
public static String commonDateTimeFormat(Date date) {
SimpleDateFormat format = new SimpleDateFormat(HOUR_FORMAT);
String str = format.format(date);
return str;
}
public static Date commonDateTimeParse(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat(HOUR_FORMAT);
try {
Date date = format.parse(dateStr);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYear(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(YEAR_FORMAT);
String format = dateFormat.format(date);
return format;
}
// 获取当前时间yyyy-MM-dd HH:mm
public static String getNowTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str = dateFormat.format(now);
Calendar calendar = Calendar.getInstance();
return str;
}
// 获取当前时间推前一个月的时间 yyyy-MM-dd HH:mm
public static String getUpMothDate() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, -1);// 减一个月
str = sdf.format(lastDate.getTime());
return str;
}
}