Not able to compare java.util.Date with Timestamp in Oracle - spring

I am not able to compare java.util.Data with Oracle Timestamp
Date stored in address table is like 29-JUL-13 07.15.57.529000000 PM
My Address class is like below
#Entity
#org.hibernate.annotations.Entity(dynamicUpdate = true)
#Table(name = "address")
public class Address {
#Id
#GeneratedValue(generator = "assigned-by-code")
#GenericGenerator(name = "assigned-by-code", strategy = "assigned")
#Column(name = "id")
private String id;
#Type(type = "timestamp")
#Column(name = "lastaccesstimestamp")
private Date lastAccessTimestamp;
getter and setter
}
#Query("from Address a where a.associationId =:associationId and a.lastAccessTimestamp >=:lastAccessTimestamp")
List<Address> findLatestUpdatedAddresses(
#Param("lastAccessTimestamp") Date lastAccessTimestamp,
#Param("associationId") String associationId);
And lastAccessTimestamp in findlatestUpdateAddress method is Calendar.getInstance().getTime()(e.g Mon Jul 29 10:10:09 IST 2013).
It is giving proper result for month, day and year compression but when there is change in minute, second or hour it is not giving proper result.
For Example if lastAccessTimestamp is Mon Jul 29 10:10:09 IST 2013 and DB date is 30-JUL-13 07.15.57.529000000 PM it will return result
but if lasteAccessTimestamp is Mon Jul 30 07:09:09 IST 2013 it is not giving any result I think hour minute and second part is not considered in compression.
In short for month date and year compression is working fine but for minute second and hour it is not working.
Any suggestion will be appreciated even if there is any alternative solution will be accepted.

Please, try to replace your:
#Type(type = "timestamp")
with
#Temporal(TemporalType.TIMESTAMP)
This annotation will tell hibernate to include hour/min/sec as it handles the object mapping.

Related

How to get the same output of departed Date.parse() in groovy?

I have an application that runs the old version of the spring application. The application has the function to create date objects using Date.parse as follows
Date getCstTimeZoneDateNow() {
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
def zonedDateString = new Date().format(dateFormat, TimeZone.getTimeZone('CST'))
Date date = Date.parse(dateFormat, zonedDateString)
return date // Tue Oct 18 20:36:12 EDT 2022 (in Date)
}
However, the code above is deprecated. I need to produce the same result.
I read other posts and it seems like Calender or SimpleDateFormatter is preferred.
And I thought SimpleDateFormatter has more capabilities.
This post helped me understand more about what is going on in the following code
SimpleDateFormat parse loses timezone
Date getCstTimeZoneDateNow() {
Date now = new Date()
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
SimpleDateFormat sdf = new SimpleDateFormat()
sdf.setTimeZone(TimeZone.getTimeZone('CST'))
// cstDateTime prints times in cst
String cstDateTime = sdf.format(now) // 2022-10-18T20:36:12.088Z (in String)
// JVM current time
Date date = sdf.parse(cstDateTime) // Tue Oct 18 21:36:12 EDT 2022 (in Date)
return date
}
Here my goal is to return the date object that is in the format of Tue Oct 18 20:36:12 EDT 2022
The format is good. However, like the post says, when I do sdf.parse(), it prints in JVM time.
This means, the format is good but the time zone is off.
How can I get the exact same result as before?
It does not have to use SimpleDateFormatter. Could be anything.
Thank you so much for reading and for your time.
Perhaps the important thing is, that the Date is always neutral to the timezone. Given example shows what is to be expected to work from the Java specs:
def format = new SimpleDateFormat()
format.setTimeZone(TimeZone.getTimeZone("CST"))
println new Date()
def date = format.parse(format.format(new Date()))
printf "parsed to %s%n", date
printf "formatted to %s (%s)%n", format.format(date), format.getTimeZone().getDisplayName()
In the output, notice when using the Format and when the toString(), a different time is shown accordingly, which is perfectly fine, since first we format and then parse again in the same format, thus the same time-zone. Later, we use the Date.toString() to output the date, this time using the system default time-zone which is always used when Date.toString() is called. In the output, the time-zone shift is reflected:
Thu Oct 20 09:22:58 EDT 2022
parsed to Thu Oct 20 09:22:00 EDT 2022
formatted to 10/20/22 8:22 AM (Central Standard Time)

How to get current time with offset in Java?

How do I get the current time in the below format. Timezone is Europe/London:-
04:47 PM GMT+1
I have tried various different ways, including below code:-
ZoneId zone = ZoneId.of("Europe/London");
Locale locale = Locale.forLanguageTag("en-GB");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"dd MMM uuuu HH:mm OOOO", locale);
ZonedDateTime dateTime = Instant.now().atZone(zone);
String result = dateTime.format(formatter);
This gets me -->30 Mar 2021 18:36 GMT+01:00. But its not what I want.
Update:- Its resolved now. This pattern helped----->> DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "hh:mm a O", locale);

OffsetDateTime java8 ojdbc8 oracle insert cet to cest problem

Hello I have a problem with insert OffsetdateTime with correct timezone I have string 20110401000000000 then I do convertion to OffsetDateTime like this:
DateTimeFormatter dft =
new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
LocalDateTime dateTime = LocalDateTime.parse(pDateTimeString, dft);
OffsetDateTime of = OffsetDateTime.of(dateTime, ZoneOffset.ofHours(1));
and I get 2011-04-01T00:00+01:00 - everything is good
now I do insert to database
ps.setObject(3,of);
and in my datebase I see
is it convert in cest
curiosity -------------
if date 2021-03-03T00:00+01:00
it is correct put in database
-- my settings of my connection
c.getDefaultTimeZone() -> Europa/Zurich
c.getSessionTimeZone() -> Europa/Zurich
Thank you for help

Java TimeZoneOffset time conversion

Using Jdk8.
I am trying to convert time actually just hours of the day (like 1130) into DateTime. I am trying it 2 ways as below none of them work correctly. 1130 gets converted into 11:00 and another 3:00 none of them are correct. Is there also a 3rd way naming my Time Zone.
Long ltime = Long.parseLong("1130");
Long seconds = (ltime/100)*60*60;
LocalDateTime tsutcDttime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
LocalDateTime lclDttime = LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneId.systemDefault());
System.out.println("ZoneId.systemDefault: " +ZoneId.systemDefault());
System.out.println("UTC LocalDateTime : "+ tsutcDttime);
System.out.println("Sys Def LocalDateTime : "+ lclDttime);
ZoneId.systemDefault: America/Los_Angeles
UTC LocalDateTime : 1970-01-01T11:00
Sys Def LocalDateTime : 1970-01-01T03:00
This problem got solved by below.
Part2 of this problem
For some of my time components I have a Date and for some other I don't (but I have to convert everything into datetime anyway.
How do I add the actual date when I have it? I am not seeing a method LocalDate (date)? For others will simply let default to 1970 unless a better soln exists.
The below just puts the 1970 date:
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(secondsOfDay, 0, ZoneOffset.UTC);
But when I have a date I would like to add it to secondsOfDay and create my DateTime- How. Like Date = Jan 1 2016 + secondsOfDay
Thanks
Assuming the string "1130" means 11 hours and 30 minutes you need to do the conversion to seconds separately for the hours and minutes:
long ltime = Long.parseLong("1130");
long hoursAsSeconds = (ltime / 100) * 60 * 60;
long minsAsSeconds = (ltime % 100) * 60;
long secondsOfDay = hoursAsSeconds + minsAsSeconds;
You can then use LocalTime to get the local time given the seconds:
LocalTime localTime = LocalTime.ofSecondOfDay(secondsOfDay);
You could also get the local time directly by parsing the time like this:
LocalTime localTime = LocalTime.parse("1130", DateTimeFormatter.ofPattern("HHmm"));
From that you can get a LocalDateTime using something like:
LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), localTime);
Or a ZonedDateTime using:
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.now(), localTime, ZoneId.of("America/Los_Angeles"));
This expression is causing the issue:
Long seconds = (ltime/100)*60*60;
Division operator returns integer value so you are loosing precision here.
1130/100 = 11.3 ~ 11
11 * 60*60 = 39600
11.3 * 60*60 = 40680
Division should be the last operation in your expression:
Long seconds = (ltime*60*60)/100;

(Swift) Using NSDataFormatter with a medium style string to get date

I seem to be stuck here and I have been wasting way too much tome on this.
What I have is a string that is in the RFC 1123 format that I would like to get a date out of, but not matter what I do, I get a nil result;
let dateFormat = NSDateFormatter();
dateFormat.dateStyle = .MediumStyle;
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z";
dateFormat.locale = NSLocale.systemLocale();
dateFormat.timeZone = NSTimeZone(abbreviation:"GMT");
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT");
I am not sure what I am missing, if I changed the MMM to MM and make Jun 06, then it works. It seems to be only this instance. I have tried moving the order of how dateFormat gets created, and still I get no results. Any help on this matter would greatly be appreciated
I think you have confused the formatter. You don't need to set anything except the format string, because the formatter's job is to learn those other settings from the string it reads.
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z"
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT")
// "Jun 27, 2015, 11:30 PM"
If you do as above, it will return an NSDate? from the date string you provided.

Resources