net.fortuna.ical4j.model.DateTime to ORACLE date - oracle

I have net.fortuna.ical4j.model.DateList which contains
net.fortuna.ical4j.model.Date objects
The output is: 20170522,20170523,20170525
(UTC time zone)
I have to convert it to ORACLE date in SystemDefault timeZone.
I tried to do this:
List<DATE> result = new ArrayList<DATE>
for(Date d : rdates){
result.add(new DATE(new Timestamp(d.getTime()));
}
But oracle date is different as expected.From net.fortuna.ical4j.model.Date 20170522,20170523,20170525 I got 20170521,20170522,20170524 ORACLE DATE.
There is shifting. How can I handle this?

Ical4j Date objects have an underlying timezone that is not defined as part of the formal specification (an implementation quirk).
By default this timezone will be UTC, however you can change this to system default using the following compatibility hint:
net.fortuna.ical4j.timezone.date.floating=true

Related

Change timezone with Carbon

I have a date that is coming from database (type date):
2018-08-25
This date is in french timezone.
When I do:
$from = Carbon::parse("2018-08-25", 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-24 22:00:00.0 UTC (+00:00)
Which is what I want
But when I use field from DB:
$operation = Operation::findOrFail($request->operation);
$from = Carbon::parse($operation->date_ini, 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-25 00:00:00.0 UTC (+00:00)
In my DB, field is saved as : 2018-08-25, so literraly, it means 2018-08-25 UTC. So result is coherent. But I'm not sure how to deal with it to get what I want. The implication would be that I have to store my date like a datetime in DB so that I can store it in UTC with 1 or 2 hours less. Is there anyway to avoid this and keep it simple ?
Any idea ?
I solved it using:
$from = Carbon::parse($operation->date_ini)->shiftTimezone('Europe/Paris');;
shiftTimezone with change timezone without changing the date. So, it do the trick for me !
If you call setTimezone() on an existing instance of Carbon, it will change the date/time to the new timezone, for example
$changeTimeZone = \Carbon\Carbon::parse($operation->date_ini)->setTimezone('Asia/Dhaka')->format('H:i');

Saving date in UTC in Oracle Timestamp column

I have a requirement to save the current UTC date with time in an Oracle column of type TIMESTAMP WITH TIMEZONE. this is from a Spring Boot service with JPA and Hibernate
I have enabled the following in my application yml
jpa:
hibernate:
ddl-auto: none
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.Oracle12cDialect
jdbc:
time_zone: UTC
and the Entity class field looks like
#Column(name = "last_user_edit_date", columnDefinition = "TIMESTAMP WITH TIME ZONE")
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private ZonedDateTime lastUserEditDate;
While setting date I am using
obj.setLastUserEditDate(ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")));
The above is working fine with respect to the actual date value. The only problem is in the database it is saving the UTC time but mentions MST (my local timezone) as the timezone. For eg a value saved is
12-SEP-19 09.50.53.820000000 PM AMERICA/DENVER
Here the 9.50 PM is actually the UTC time but the timezone comes as AMERICA/DENVER. What i want is
12-SEP-19 09.50.53.820000000 PM UTC
How can i achieve this Spring JPA and with Java 8 classes?
Thanks
LocalDateTime is the wrong class
The LocalDateTime class is not capable of tracking a moment in time. I cannot imagine a scenario where calling LocalDateTime.now() makes sense. Read the Javadoc before using a class.
Track a moment: Instant, OffsetDateTime, ZonedDateTime
To track a moment, use Instant (always in UTC), OffsetDateTime (a moment with an offset-from-UTC), or ZonedDateTime (a moment as seen in a particular region).
Oddly, JDBC 4.2 requires support for OffsetDateTime yet leaves the most common two classes, Instant & ZonedDateTime, optional.
So, to capture the current moment in UTC for JDBC work:
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
Or the longer:
Instant instant = instant.now() ; // Capture current moment in UTC.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
Send to database:
myPreparedStatement.setObject( … , odt ) ;
Retrieve from database:
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
JPA
I do not use JPA. But it looks this Question has you covered, JPA Storing OffsetDateTime with ZoneOffset. And see this post, What’s new in JPA 2.2 – Java 8 Date and Time Types.
Other time zones & offsets
only problem is in the database it is saving the UTC time but mentions MST
This documentation for Oracle Database seems to say that TIMESTAMP WITH TIME ZONE type does record the incoming data’s time zone or offset-from-UTC. Some other databases such as Postgres adjust incoming values to UTC (an offset of zero hours-minutes-seconds).
To get UTC, retrieve the OffsetDateTime as seen above, and call toInstant method to produce a Instant object which is always in UTC. Or produce another OffsetDateTime that is definitely in UTC:
OffsetDateTime odtUtc = odt.withOffsetSameInstant​( ZoneOffset.UTC ) ;

Spring data mongodb: Work with dates, date has 2 hours of difference

I've this document stored in mongo:
{
"_id" : "cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d",
"expiresAt" : ISODate("2018-10-10T00:00:00Z")
}
In order to get sure, all data is right, I've got first the document and I've log some data:
Reference ref = this.mongoTemplate.findById("cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d", Reference.class);
LOG.info(ref.getExpiresAt().toString());
LOG.info(Long.toString(ref.getExpiresAt().getTime()));
The result is:
Wed Oct 10 02:00:00 CEST 2018 <<<<<<<<<<<<<<<<<<<
1539129600000
As you can see when I get the object, the expiresAt field is 02:00:00 instead of 00:00:00.
Value in database is expiresAt field is: ISODate("2018-10-10T00:00:00Z")
Any ideas or thoughts welcome for this issue!
This date is in Zulu time (note the 'Z' on the end):
ISODate("2018-10-10T00:00:00Z")
When you do this, specifically the call to .toString(), you are converting the date into a local date string in your time zone, which appears to be Zulu+2:
LOG.info(ref.getExpiresAt().toString());
I usually set my server's time zone to UTC/Zulu/GMT, in order to avoid any automatic timezone conversions happening like this.

How do I change the time format and data format in logstash?

I want to load mysql data using logstash and put it into elasticsearch.
I added the "jdbc:mysql://local:portnumber/test?zeroDateTimeBehavior=convertToNull" option to load the data from mysql.
The time format is changed when getting the value of the date field in mysql.
for example....
date field value : 2017-04-07 08:01:00
logstash value: 2017-04-06T23:01:00.000Z
I want to change the time format to Asia / Seoul (GMT + 9) format.
So I tried the below, but the format does not changes.
date{
match => ["reg_date","yyyy-MM-dd HH:mm:ss.S"]
#timezone => "UTC"
timezone => "Asia/Seoul"
}
I will change the format to 'yyyy-MM-dd HH: mm: ss.S' using 'ruby code' after changing the time format with 'date filter'.
How do I change the time format?
Logstash sets the timestamp always to UTC. You should change the timestamp on the presentation layer instead to another timezone.

updating time of datetime field using linq

I have two fields in my database for storing starttime and endtime. they are of datetime. I pick time from them using tostring("hh:mm tt"). Now I want to update only the time part of the date. I have dropdownlist to select hour and minutes and AM/PM. How can I update the time of date stored in sql server using Entity framework / LINQ in MVC3 application.
Please suggest
actually you don't really need to touch the linq part of this, basically what you want to do is just to convert the string back to a datetime object and just manipulate the datetime object by either creating a new datetime object or add/minus mm/dd/yy hours or minutes.
You must always update the whole datetime - it means you must build a new DateTime in your application and use correct Date part and defined Time part.
create new DateTime Object
DateTime mydate = New DateTime(2011, 6, 1, 12, 30, 0);
or
DateTime mydate = DateTime.Parse("2011-06-1 12:30:00 PM");

Resources