Date value wrongly formatted

前端 未结 3 1319
悲哀的现实
悲哀的现实 2021-01-21 12:03

I am trying to convert a String DateTime value which is present in a flat file as a Date object after parsing the flat file in my code.

I have

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 13:00

    Can you please let me know whats the issue here.

    Sure. You're effectively calling date.toString(), which doesn't know anything about the SimpleDateFormat which was used to parse the original text value. A Date is just an instant in time. It has no notion of a per-instance format. Additionally, it doesn't know about a time zone. You've given a value in PDT, which was then parsed... and when you print it, it's using the system local time zone (IST). That's what Date.toString always does.

    If you want to format a Date in a particular way, using a particular format in a particular time zone, call DateFormat.format.

提交回复
热议问题