公共日期時間操作類
public class DateTime{
public static final String DateTimeFortmat1 = "yyyy-MM-dd HH:mm:ss";
//返回指定格式的當前日期時間
public static String currentDateTime(String format){
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(d);
}
//把long類型轉換成 日期類型date
public static Date long2Date(final long millisecond){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millisecond);
return calendar.getTime();
}
//比較2個時間。 如果endTime 晚於beginTime ,則返回true,否則返回false
public static boolean compareTime(String beginTime,String endTime){
long begin = DateTime.dateTimeParse(beginTime,DateTimeFortmat1);
long end = DateTime.dateTimeParse(endTime,DateTimeFortmat1);
if(end > begin ){
return true;
}
return false;
}
//返回文本信息的日期 對應的格林威治標準時間的偏移量,單位是毫秒,1s=1000ms
//datetime="2019-04-12 17:16:58" format="yyyy-MM-dd HH:mm:ss"
public static long dateTimeParse(String datetime,String format){
SimpleDateFormat sdf = new SimpleDateDateFormat(format);
Date date = sdf.parse(datetime);
return date.getTime();
}
}