String,Date,Long三种类型之间相互转换

字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
通常我们取时间跨度的时候,会substring出具体时间--long-比较


String–>Date–>Long

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");
Date date = sdf.parse("2016/11/18 10:31:37 AM");//String-->Date
Long dateLong = date.getTime();//Date-->Long

Long–>Date–>String

Long long = 1654651313;
Date date = java.sql.Date.valueOf(long);//Long-->Date
Date date = new Date(long);//Long-->Date(常用)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string=sdf.format(date);//Date-->String

Calendar–>Date–>Calendar

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();//calendar-->Date
calendar.setTime(date);//Date-->calendar

注意:calendar可以将日期加1

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,1);
Date date1 = calendar.getTime();

你可能感兴趣的:(日期)