skip to Main Content

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


  1. you can use LocalDateTime for this , you should avoid using legacy Date class

     String str = "Mon Sep 02 00:00:00 WIB 2019";
                LocalDateTime parse = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
                System.out.println(parse);
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String format = parse.format(dateTimeFormatter);
                System.out.println(format);  
                // out-put 2019-09-02
    

    You can also use same on @JsonFormat(pattern = "yyyy-MM-dd")

    Login or Signup to reply.
  2. tl;dr

    ZonedDateTime
            .parse(
                    "Mon Sep 02 00:00:00 WIB 2019" ,
                    DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US )
            )
            .toLocalDate()
    

    2019-09-02

    Table of all date-time types in Java, both modern and legacy

    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.

    String input = "Mon Sep 02 00:00:00 WIB 2019";
    Locale locale = Locale.US;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , locale );
    

    Parse.

    ZonedDateTime zdt = ZonedDateTime.parse( input , f );
    

    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.

    System.out.println( "zdt = " + zdt );
    

    zdt = 2019-09-02T00:00+07:00[Asia/Jakarta]

    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?

    LocalDate ld = zdt.toLocalDate() ;
    

    Or do you want the date as seen in UTC?

    LocalDate ld = 
        zdt
        .toOffsetDateTime()
        .withOffsetSameInstant( ZoneOffset.UTC )
        .toLocalDate() 
    ;
    

    Then generate your desired output string whose format happens to comply with the ISO 8601 standard format used by default in LocalDate::toString.

    String output = ld.toString() ;
    

    Tip: Educate the publisher of your data about:

    • The ISO 8601 standard defining date-time formats to use when exchanging date-time values as text.
    • The wisdom of generally communicating moments in UTC (an offset of zero hours-minutes-seconds) rather than a particular time zone.
    • Real time zone names are in the format of Continent/Region, not 2-4 letter pseudo-zones such as WIB, CST, or IST.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search