Setting timezone = "Asia/Kolkata" in #jsonformat and storing it in java.sql.timestamp converts 12 pm to 1 pm timesatmp to 00 am in spring hibernate - spring-data-jpa

I am using
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss", timezone = "Asia/Kolkata")
private Timestamp startDateTime;
to store timestamp comes in json as a string.
But the problem is it converts time between 12 pm to 1 pm into 00 am.
E.g. 2021-10-25 12:30:00 gets converted to 2021-10-25 00:30:00.
Any help will be appreciated.
Thank you.

The root cause of the problem is that you have used h instead of H. Note that h is used for 12-hour time format (i.e. time with AM/PM marker) while H is used for 24-hour time format. So, the solution to your problem is to change the format to dd-MM-yyyy HH:mm:ss.
In case, you want the AM/PM marker in the time, change the format to dd-MM-yyyy hh:mm:ss a.

Related

Flutter Convert date time to string failed midnight

I am trying to convert date and time to string with below code but when I convert the below
2020-09-01 00:00:00.000 I get 01-09-2020 24:00 that is wrong
String formattedDate = DateFormat('dd-MM-yyyy kk:mm').format(date);
the correct is 01-09-2020 00:00 how can I get this
Any ideas?
String formattedDate = DateFormat('dd-MM-yyyy kk:mm').format(date);
I have no idea what "kk" is as a format string, but you seem to want "HH" for "hours":
String formattedDate = DateFormat('dd-MM-yyyy HH:mm').format(date);
Please try this, it's work for me :
DateFormat('dd-MM-yyyy hh:mm').format(DateTime.parse("2020-09-01 00:00:00.000"))
The two formats essentially do the same thing but differ in how they
handle midnight. kk will format midnight to 24:00 whereas HH will
format to 00:00. The hours in a day in k are 1-24 and in H are 0-23

DateFormatter giving me incorrect result while I am converting String into Date in UK region and Timezone is Muscat

I want "yyyy-MM-dd HH:mm:ss", with time in 24 hour format, but as i m having 12 hrs date format setting in my phone and Timezone is set to the Muscat ,the date which I am getting is always 12 hrs format, while i m checking with UK region. I am able to change Date() into string in 24 hrs format but while I am changing 24 hrs string into 24 hrs Date , It always give me 12 hrs Date format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let dateToString = dateFormatter.string(from: Date()) //2020-04-06 15:47:16
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let dateFromString = dateFormatter.date(from: dateToString)
print(dateFromString) // 2020-04-06 1:47:25 pm +0000
A Date object just represents a point in time. The “should I show it in 12 hour clock or 24 hour clock” is not something that Date objects know about. This is a feature of strings generated by (or consumed by) a DateFormatter.
So, a few thoughts:
A date formatter’s dateFormat (and its locale) dictates what a string will look like when you call string(from:). (It will also dictate how to interpret a string and create a Date object when you call date(from:), but that’s not relevant here.)
So, if you’re looking for a string representation of a date using a 24 hour clock, look at the string generated by the date formatter’s string(from:) method. This is the dateToString string in your example.
But, if you subsequently generate a Date object from the formatter’s date(from:) method, that resulting Date will not capture whether to use 12 vs 24 hour clock. If you print this Date object, it won’t reflect your 12/24 hour clock preference.
Bottom line, only concern yourself with am/pm vs 24-hour clock when looking at String objects generated by (or passed to) the DateFormatter. Don’t worry about the format of the output when you print a Date object, as that’s for debugging purposes only and won’t capture this am/pm vs 24-hour clock dimension.
You said:
Final statement is giving me “2020-04-09 4:23:27 am +0000” format.
If you’re seeing the +0000, that suggests that you are printing the Date object, itself. The dateFormat and locale of a DateFormatter only controls the format of the string generated by string(from:) (and how strings are parsed).
So, print the string generated by DateFormatter, not Date objects. The print of a Date object will always be in this predefined format. Within the app, if you need the output in a given format, use the String generated by DateFormatter, not Date objects.
Consider a more obvious example of the issue where the DateFormatter is used to create a string in a very different format:
let now = Date()
print(now) // 2020-04-07 11:54:58 +0000
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .long
let string = formatter.string(from: Date())
print(string) // April 7, 2020 at 4:54:58 AM PDT
if let date = formatter.date(from: string) {
print(date) // 2020-04-07 11:54:58 +0000
}
So, even though that formatter successfully converted a Date to a String, and back, that final print statement uses the same fixed format that the first print statement did, because I’m just printing a Date object. I’m not concerned that the that last print statement didn’t honor the configuration of my DateFormatter. I wouldn’t expect it to. When I print a Date, it’s always in that fixed, predefined format. I only worry about the format of the strings generated by the DateFormatter (the second print statement).
As an aside, there are secondary questions about your "yyyy-MM-dd HH:mm:ss" format. What timezone does this represent? Your local timezone? Your server’s timezone? GMT/UTC/Zulu? We often use ISO8601/RFC3339 date strings (like 2020-04-07T11:54:58Z) to remove this ambiguity. You should get your arms around your original question first, but when you have that behind you, you’ll want to take a hard look at why you’re storing it in this format, and how you want to deal with timezones correctly. But first things first.

How to convert date from one format to another? Need to get date to "Tue Apr 3 22:10:06 2018" format

How to convert "2018-04-03 22:10:06" to "Tue Apr 3 22:10:06 2018"? Obviously not those specific dates but that format.
I found this solution:
How to convert date format from dd/MM/YYYY to YYYY-MM-dd in swift
but I am unable to get it to the exact format.
So, I just threw this into Playgrounds
let inFormat = DateFormatter()
inFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = inFormat.date(from: "2018-04-03 22:10:06")
let outFormat = DateFormatter()
outFormat.dateFormat = "E MMM d HH:mm:ss yyyy"
outFormat.string(from: date!)
It eventually outputs Tue Apr 3 22:10:06 2018
The formats were initial referenced from nsdateformatter.com
This, of course, will use the current system TimeZone and Locale, so you may want to do some more investigation into that

Is DateFormatter class broken in swift 3?

I am trying to use Date class which is provided from Swift 3 library. I am not sure if I am doing it right.
When I print Date it prints correct date, but when I try to convert it from date to string it changes the date to something else.
let today = Date()
print(" Date object : \(today)")
let format = DateFormatter()
format.dateFormat = "mm/dd/yy"
print(" Date to String : \(format.string(from: today)")
Which gives the output:
Date object : 2017-06-03 18:13:39 +0000
Date to String : 13/03/17
mm is the format specifier for minutes, hence why the output returns 13 instead of 06, which is the time in minutes at which you called Date().
You'll need to use MM to display the month.
See the unicode report on date specifiers for more information: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
No it doesnt.
Your format is Minute, Day and Year.
Works exactly as it should.
Try MM istead of mm.

NSDate from NSString when time is in 24 hr format

I have a string of the format Jan 25, 2011 3:17 AM. I need to convert it to NSDate.
I used NSDateFormatter with format #"MMM d, yyyy h:mm a". It Works well if iphone time is in 12 hr format, but returns nil if time is in 24 hr format. Can any one help me with this????
Capital H is used for 24 hour format. Don't 24 hour times usually exclude the AM/PM part? If so, your format string should be: #"MMM d yyyy H:mm".
Here's a reference for Unicode date format strings.
It's a bug in NSDateFormatter. You can work around it by manually setting a locale on the date formatter:
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"] autorelease]];