SimpleDateFormat.格式返回错误的日期 [英] SimpleDateFormat. format returning wrong date

查看:59
本文介绍了SimpleDateFormat.格式返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code I would like to know why it returns correct date

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

推荐答案

尝试使用此方法:

   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);

这篇关于SimpleDateFormat.格式返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆