Expand my Community achievements bar.

SOLVED

Date format in aem

Avatar

Level 5

How to get current date in this date format "2021-05-19T18:27:16.944+05:30" in Java script or Java?

I checked but all were in this format "2021-06-29T04:37:27.796Z". Can anyone tell me what should be format?

 

Thank you

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Did you try java.time.OffsetDateTime ?

you can refer this code snippet

 

public static void main(String[] args) {
        String strDateTime = "Tue Jun 29 15:37:43 GMT+05:30 2021";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
      
        OffsetDateTime formatedDate= OffsetDateTime.parse(strDateTime, dtfInput);

        System.out.println(formatedDate);

    }

 

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @keerthana_hn 

 

Please see the link here:

https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#1222-dates

 

${'yyyy-MM-dd HH:mm:ss.SSSXXX' @ format=properties.date, timezone='GMT+05:30'}

 

Thanks!

Avatar

Correct answer by
Community Advisor

Did you try java.time.OffsetDateTime ?

you can refer this code snippet

 

public static void main(String[] args) {
        String strDateTime = "Tue Jun 29 15:37:43 GMT+05:30 2021";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
      
        OffsetDateTime formatedDate= OffsetDateTime.parse(strDateTime, dtfInput);

        System.out.println(formatedDate);

    }

 

Avatar

Community Advisor

Hi @keerthana_hn ,

 

You can use below code-

 

 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MyDateExample {

public static void main(String[] args) {
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
     sdf.format(new Date());
     System.out.println("sdf"+sdf.format(new Date()));
}

}

 

 

Result - 2021-06-29T11:39:25 +0530