Date format Mapping to JSON Jackson Not working properly - jersey

I have a Date format coming from API like this:"date": 1498729813872
But actually i want in dd-M-yyyy hh:mm:ss in this way
i am using jersey and jackon for data serialising how can i change date in seconds for mat to specific format

Disable serializing dates as timestamps on Jackson's ObjectMapper. E.g.
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Annotate the date field appropriately. E.g.
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-M-yyyy hh:mm:ss")
private Date date;
or also set the format on the ObjectMapper
DateFormat df = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
mapper.setDateFormat(df);
Docs for JsonFormat

Related

Select Datetime as Date in criteria query

I'm using JPA CriteriaBuilder to select attributes from the database. I have a specific use case that involves Conversion of datetime to date. The Projection segment of the code is as follows.
CriteriaQuery<ResponseDTO> query = cb.createQuery(ResponseDTO.class);
query.multiselect(root.get(MasterPackagesV1_.masterPackageName),
packageJoin.get(PackagesV1_.packageId),
packageJoin.get(PackagesV1_.packageName),
packageJoin.get(PackagesV1_.startSellingDate),
packageJoin.get(PackagesV1_.endSellingDate),
packageJoin.get(PackagesV1_.createdDate),
packageJoin.get(PackagesV1_.modifiedDate),
packagePaymentJoin.get(PackagePaymentPlanV1_.mrp),
packageSubscriptionJoin.get(PackageSubscriptionsV1_.packageSubscriptionId)
);
All of the date attributes are stored as datetime datatype in the MySQL database. I'm want the datetime to be formatted to date. i.e Instead of retrieving the Dates as LocalDateTime I want it to be converted to LocalDate during Instantiation.
I have made changes in the ResponseDTO class accordingly to support LocalDate. Thanks in Advance.

#JsonFormat Date field to localtimezone

I have a field called expiredDate. The value of this field in the DB is: 2019-11-06 22:48:04. I am using SpringBoot and I have defined the field in POJO as:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MMM-dd HH:mm:ss")
#JsonProperty("expiration_date")
Date expirationDate;
When I retrieve the date I get the following: 2019-Nov-07 04:48:04. My requirement is to get it in the following format: 2019-11-06T22:48:04.000Z.
I have given the timezone='CST' but I dont want to mention the timezone. I want my program to identify the timezone and show the result.
If I dont give the #JsonFormat annotation, I get the result in long.
What am I missing here?

How can you make LocalDate to return Date in a specific format?

How can you make LocalDate to return Date in a specific format ?
Ex . LocalDate ld = LocalDate.now();
The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 .
** Dont say use formatter because formatter will not return date , it will return String which is of no use for me .
Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A date will never have only one accepted format. So yes, for representing a date as string, users should apply any customized formatter.
But I suspect that you want to change the behaviour of the LocalDate-method toString() which produces a string in the ISO-format "2018-11-24". Well, you cannot because the class LocalDate is final so overriding this method is impossible, and there is also no configuration hook to change the behaviour because this would be in conflict with the immutability of the class.

How can we convert com.datastax.driver.core.LocalDate to YYYY/MM/DD format?

I want to convert the com.datastax.driver.core.LocalDate object returned by the API to yyyy-MM-dd format. How can I do that?
I tried below code but it is not working for com.datastax.driver.core.LocalDate-
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
String date;
I am not sure how you can do it using annotation but below is one way of explicitly converting com.datastax.driver.core.LocalDate to yyyy-MM-dd format -
java.util.Date dateObj = new java.util.Date(localDate.getMillisSinceEpoch());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(dateObj);

Java8- ZonedDateTime with DateTimeFormatter not recognizing the format

I am using the ZonedDateTime with DateTimeFormatter of Java 8. When I try to parse my own pattern it doesn't recognizes and throws an exception.
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
ZonedDateTime dt = ZonedDateTime.parse(oraceDt, formatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '1970-01-01 00:00:00.0' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1970-01-01T00:00 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at com.timezone.Java8DsteTimes.testZonedDateTime(Java8DsteTimes.java:31)
at com.timezone.Java8DsteTimes.main(Java8DsteTimes.java:11)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1970-01-01T00:00 of type java.time.format.Parsed
Well, you want to create a ZonedDateTime which always refers to a timezone but your input does not contain such an information, and you have also not instructed your formatter to use a default timezone if the input is missing a zone. There are two solutions for your problem:
Instruct your parser to use a timezone (here using the system tz as example):
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
ZonedDateTime zdt =
ZonedDateTime.parse(oraceDt, formatter.withZone(ZoneId.systemDefault()));
System.out.println(zdt); // in my default zone => 1970-01-01T00:00+01:00[Europe/Berlin]
Use another result type which does not need a timezone (here LocalDateTime):
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
LocalDateTime ldt = LocalDateTime.parse(oraceDt, formatter);
System.out.println(ldt); // 1970-01-01T00:00
Here is an small example of using the ZonedDateTime with DateTimeFormatter of Java 8.
public String currentDateTime() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE y-MM-d MMMM HH:m:s z Z'['VV']' 'Day:'D");
return ZonedDateTime.now(ZoneId.of("Europe/Lisbon")).format(dateTimeFormatter);
}
The ZonedDateTime class permits you to create a date/time object with a time zone. The default time zone will be used; i.e., the time zone you have established on your computer. You can use the DateTimeFormatter class to display the time zone. In the Java code, the time zone is displayed as zone offset, zone name and zone ID. Note that the date time formatter pattern is "Z", "z", and "VV", respectively. The program also creates a date/time object and then adds the zone ID using the of method of the ZoneId class. Finally, a time zone is added to another date/time object using the of method of the ZoneOffset class.

Resources