Java将字符转换为时间类型在转换为日期类型,c.setTime(date)是日期类型吗?为什么会报错
public class TestCalendar2 {
public static void main(String[] args) throws ParseException {
String str="2020-10-10";//字符串
DateFormat df =new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(str);//字符串转时间类型
Calendar c=new GregorianCalendar();
c.setTime(date); //时间类型转日期类型
System.out.println(date);//System.out.println(df.parse(str));
System.out.println(c);// System.out.println(c.setTime(date));
}
} 展开
没有报错啊。这是拿到的结果:
run:
Sat Oct 10 00:00:00 MYT 2020
java.util.GregorianCalendar[time=1602259200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Kuala_Lumpur",offset=28800000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=9,WEEK_OF_YEAR=41,WEEK_OF_MONTH=2,DAY_OF_MONTH=10,DAY_OF_YEAR=284,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
BUILD SUCCESSFUL (total time: 0 seconds)