Java8- ZonedDateTime with DateTimeFormatter not recognizing the format - java-8

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.

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.

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 i change timezone in neo4j?

I want to change default timezone from UTC to another timezone. How it will be possible? I tried
RETURN apoc.date.format(timestamp(),'ms', 'yyyy/MM/dd HH/mm/ss')
but it returns utc time on localhost.
Example answer:
RETURN apoc.date.format(timestamp(),'ms', 'yyyy/MM/dd HH:mm:ss',"Europe/Paris")
Method from the source:
public String format(final
#Name("time") Long time
,#Name(value = "unit", defaultValue = "ms") String unit
,#Name(value = "format",defaultValue = DEFAULT_FORMAT) String format
,#Name(value = "timezone",defaultValue = "") String timezone)
{
return time == null ? null : parse(unit(unit).toMillis(time), format, timezone);
}
The timezone parameter is further passed to Java TimeZone class constructor.
From Java documentation as to what it should contain -
the ID for a TimeZone, either an abbreviation such as "PST", a full
name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00".
Note that the support of abbreviations is for JDK 1.1.x compatibility
only and full names should be used.
The source code of this project -
https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/3.3/src/main/java/apoc/date/Date.java
For more info, have a look at this article about dates in APOC
http://xclave.azurewebsites.net/2018/02/28/better-know-apoc-apoc-3-date-parse-format/

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);

Date format Mapping to JSON Jackson Not working properly

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

Resources