public static void main(String[] args) throws Exception{
String time = "2018-05-12 12:30:59";
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
Long time1 = parse.getTime();
System.out.println(time + " >> " + time1);
}
将时间戳转换成日期格式:
public static void main(String[] args) {
Long time = 1527816283000L;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time1 = sdf.format(new Date(time));
System.out.println(time + " >> " + time1);
}
将时间转换为时间戳:
public static String dateToStamp(String s) throws ParseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
时间戳转换为时间:
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
Date转String:
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ds=df.format(d);
String转Date:
String ds=new String("2017-06-09 10:22:22");
Date sd=ds.parse(ds);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date sd1=df.parse(beginTime);
Date sd2=df.parse(endTime);
System.out.println(sd1.before(sd2));
System.out.println(sd1.after(sd2));