SimpleDateFormat。格式返回错误的日期

时间:2019-03-28 10:25:37

标签: java date format simpledateformat

我有此代码,我想知道为什么它返回正确的日期

Festival f= (Festival) festival.get(0);
Date d=f.getSDate();


System.out.println(d.getYear());
System.out.println(d.getMonth());
System.out.println(d.getDate());


SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); // define your format.
String formattedDate = df.format(d);
System.out.println("New Date To Show::"+formattedDate);

输出是这个

2019
2
27
New Date To Show::27/03/3919

2 个答案:

答案 0 :(得分:2)

尝试使用此:

   Date date=new Date(); 
   // you can assign your return from the function here



    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    System.out.println(localDate.getMonthValue());
    System.out.println(localDate.getDayOfMonth());
    System.out.println(localDate.getYear());


    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String formattedDate = df.format(date);
    System.out.println("New Date To Show::"+formattedDate);

答案 1 :(得分:0)

这是因为月份(d.getMonth())从零开始,而2实际上是3