I want to convert Date Format from this EEE MMM dd HH:mm:ss zzz yyyy
to like this yyyy-MM-dd
this is my data json right know from redis
{
"bannerId":4,
"bannerName":"banner test test 2 123"
"startDate":"Mon Sep 02 00:00:00 WIB 2019",
"endDate":"Sat Sep 28 00:00:00 WIB 2019"
}
and i want to convert startDate and endDate like json below
and json ouput that i want
{
"bannerId":4,
"bannerName":"banner test test 2 123"
"startDate":"2019-09-02",
"endDate":"2019-09-28"
}
so far I’ve tried it, but failed..
show error like this
failed Unparseable date: "2019-09-02
.
and this is my code
Banner.java
@Getter @Setter @NoArgsConstructor
public class Banner {
private Integer bannerId;
private String bannerName;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "EEE MMM dd HH:mm:ss zzz yyyy", timezone = "Asia/Jakarta")
private Date startDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "EEE MMM dd HH:mm:ss zzz yyyy", timezone = "Asia/Jakarta")
private Date endDate;
}
BannerController.java
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
listRedis = bannerService.loadBannerRedis(Constant.DVC_BANNER_MOBILE, Constant.LOC_BANNER_HOME, Constant.PST_BANNER_MAIN);
for (Banner banner : listRedis) {
banner.setStartDate(
new SimpleDateFormat("yyyy-MM-dd").parse(
Convert.convertDate(dateFormat.format(banner.getStartDate()))));
}
Convert.java
public static String convertDate(String dd) throws ParseException {
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = (Date)formatter.parse(dd);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE);
return formatedDate;
}
so, i need solution what im to do??
2
Answers
you can use
LocalDateTime
for this , you should avoid using legacyDate
classYou can also use same on
@JsonFormat(pattern = "yyyy-MM-dd")
tl;dr
java.time
You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.
Define a formatting pattern to match your input. Specify a
Locale
to determine the human language and cultural norms to use in parsing name of month, name of day, and so on.Parse.
Dump to console, using standard ISO 8601 format for generated text, wisely extending the standard to append the name of the time zone in square brackets.
You want the date only, without time-of-day and without the time zone.
But you must understand that for any given moment the date varies around the world by time zone. So at any moment it can be “tomorrow” in Tokyo Japan while still “yesterday” in Toledo Ohio US.
Do you want the date as seen in Indonesia?
Or do you want the date as seen in UTC?
Then generate your desired output string whose format happens to comply with the ISO 8601 standard format used by default in
LocalDate::toString
.Tip: Educate the publisher of your data about:
Continent/Region
, not 2-4 letter pseudo-zones such asWIB
,CST
, orIST
.