I have the following field in my Java class to record timestamps:
#JsonProperty
#JsonFormat(pattern = "YYYY-MM-dd hh:mm:ss", timezone = "GMT")
private Date timestamp;
This object is created and persisted in a database. This works fine, but when fetching the record from the DB which contains the correct date, Jackson de-serializes the JSON incorrectly by always setting the month and day to 12-30.
e.g. if I create a record with this timestamp:
2020-01-02 12:30:00
Jackson will deserialize it as:
2020-12-30 12:30:00
This happens every time and I don't know what's causing it. Can anyone help?
Your pattern is mixing up minutes vs. months among other things. See the doc for SimpleDateFormat
You probably want: #JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT")
Related
I am receiving a timestamp like : 07/23/2019.08.45 from a system.
I need to convert this to Epoch.
I was not able to parse this format by using SimpleDateFormat.
I tried:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08.45");
but it didn't work
I also tried to split and parse the time and date but still got the unable to parse error
Can someone please help.
What would be the most efficient way to get this done.
You are not providing the correct date format:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08:45");
Notice : instead of .
I have a date in string format like "2017-11-16" or "2017-11-16 12:59:11.243". I have to convert it to Date.
Below is my code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
Date createdOn = formatter.parse(date);
It works fine when the date is "2017-11-16 12:59:11.243". But not able to format with "2017-11-16" date.
String date is coming from URL, so it could be either with time or without time. How can I convert it into Date in spite of the fact what type of date is coming from URL?
Actually, I have to find row from the database based on the date. In the database, the date is storing in the format like 2017-11-16 12:59:11.243. But If from URL 2017-11-16 is coming then it should find according to this date.
You can modify the pars based on the presence of time,
if(dateOrDataTime.contains(":")){
//Use parser with date-time. & fetch with equal
}else{
//Use parser for date only. & fetch using date(date_time)
}
I'm trying to upload the current UTC time to a date "field" in Parse. How can I do this?
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("utc"));
String utcTime = df.format(new Date());
message.put("lastReplyUpdatedAt", utcTime);
message.saveInBackground();
Unfortunately, I can't seem to get the right imports to work for the classes above so I can't construct my String. What can I do here? Also I'm not sure if Parse would even accept that String. Are there any special formats I need to consider?
Pass a Date
From the Parse platform documentation, pass a Date object rather than a String.
Date myDate = new Date();
message.put("lastReplyUpdatedAt", myDate);
message.saveInBackground();
so i have a mssql database with a DateObjectCreated column of type DateTime. The values it will accept into the table are in the format 2013-12-23 12:23:56.567. However, my java program is creating a joda DateTime object with the format 2013-12-23T 12:23:56.567Z. this wont insert into my db. i need to either convert "2013-12-23T 12:23:56.567Z" to 2013-12-23 12:23:56.567 or find a way to allow my db table to accept "2013-12-23T 12:23:56.567Z" format
any help on this matter will be much appreciated
Many thanks
Billy
Controller
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it
DateTime dt = new DateTime(formattedDate);//here it adds the 'T' and 'Z'
ive tried
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it
Date dt = formatter.parse(formattedDate);//here it gives me the same as new Date()
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse(date);
2.enter link description here
The TO_DATE function can be used in Oracle/PLSQL. For example:
TO_DATE('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003
TO_DATE('070903', 'MMDDYY') would return a date value of July 9, 2003
TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002
Your DB column has a DateTime type. The text representation of your date is irrelevant for persisting it.
Regardless of the API you are using (JDBC, JPA, Hibernate) there will be something like a "setDateTime(Date date)" method that allows you to pass in a java.util.Date or java.sql.Date or a Java long. You can use the milli-second value of your Joda DateTime object to create whatever is required by the API.
You are working way too hard.
Convert your Joda-Time DateTime object to a java.util.Date. Just call toDate() method.
Pass Date instance to your framework or SQL.
The answer by Ralf is correct in its first part, but is wrong in the end in that you need not deal with milliseconds. Joda-Time knows how to convert to java.util.Date.
org.joda.time.DateTime now = new org.joda.time.DateTime();
java.util.Date nowAsJavaUtilDate = now.toDate();
By the way, the java.sql.Date class is simply a very thin subclass of java.util.Date. So don't let that throw you.
In the future, this will become even easier. Java 8 brings the new JSR 310 classes in the java.time.* package. These classes supplant the mess that is java.util.Date/Calendar. They are inspired by Joda-Time but are entirely re-architected. As they are a built-in part of Java, expect JDBC and related frameworks to be updated to handle these new types directly.
I am working with JodaTime now and have a question about how to parse String into DateTime.
I have got a date String in the format:
"2013-05-14T11:36:08+0000"
I tried to convert it into JodaTime's DateTime object:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dateTime = fmt.parseDateTime("2013-05-14T11:36:08+0000");
It works fine except that if I call
dateTime.getHourOfDay()
It returns me 12 instead of 11.
Surprisingly, if I use the Java's SimpleDateFormat:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = df.parse("2013-05-14T11:36:08+0000");
The date contains exactly the same result, the hour is 12 instead of 11.
I am based in London. I started to think whether this is because the summer saving time? Or did I make an mistake on how the String should be parsed?
Please help. Many thanks.