This question already has answers here:
Conversion of string with AM/PM date-time, from Oracle database
(3 answers)
Closed 6 years ago.
i want to parse oracle timestamp (01-MAY-12 01.00.47.000000000 PM) to java.util.Date
i used this:
Date dateStart=new SimpleDateFormat("yy-MM-dd HH:mm:ss.S").parse("01-MAY-12 01.00.47.000000000 PM");
but i get this error
java.text.ParseException: Unparseable date: "2012-5-1.13.0. 47. 0"
You shouldn't have to parse anything. Use one of the ResultSet.getTimestamp() methods, and you'll have a java.sql.Timestamp object directly, which extends java.util.Date.
java.sql.Timestamp ts = myResultSet.getTimestamp( … );
And this will have the additional advantage of being portale across databases and locales.
"yy-MM-dd"?
"01-MAY-12"
Is your day number really "12" and your year "01"?
And how come your error shows "2012-5-1.13.0. 47. 0", which is presumably a date in yet another format?
If you are trying to access it using JDBC then as #JB Nizet suggested use getTimestamp() or if you just have String and need to parse to Date then do it by following
Try with following format
01-MAY-12 01.00.47.000000000 PM
yy-MMM-dd hh.mm.ss.SSSSSSSSSS a
Working demo
Related
This question already has answers here:
Convert String to Date format in andorid
(2 answers)
Closed 1 year ago.
I'm trying to convert timestamps to date, I got this exception:
java.text.ParseException: Unparseable date: "1604328483716"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
All the timestamps values that I have, having a format like this 1604328483716
Your formatter is set up to handle the format "EEE, d MMM yyyy HH:mm:ss Z". "1604328483716" isn't remotely in that format.
The value "1604328483716" looks like the string version of a milliseconds-since-The-Epoch value. If so, convert it to a long (Long.parseLong) and use new Date(theLongValue), which will give you a Date instance for Monday November 2nd 2020 14:48:03 GMT (or whatever that is in your local timezone).
You might also consider using the newer date/time API in the java.time package, rather than java.util.Date.
1604328483716
It is timestamp: https://www.unixtimestamp.com/?ref=dtf.ru
So, simply do:
long modificationTime = rec.getJsonNumber("modificationTime").lngValue();
Date date = new Date(modificationTime);
This question already has answers here:
LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]
(7 answers)
How do I get a Date without time in Java?
(23 answers)
Closed 3 years ago.
I want to get a Date without time, but always failed.
below is my codes:
long curLong = System.currentTimeMillis();
curLong = curLong - curLong % TimeUnit.DAYS.toMillis(1);
Date date = new Date(curLong);
System.out.println("date = " + date);
the output:
date = Mon Oct 28 08:00:00 CST 2019
anyone knows why? Thank you
It is not recommended to use java.util.Date anymore. It was called Date but doesn't necessarily hold only the date information but information about the time additionally.
Use this:
LocalDate today = LocalDate.now();
and print it as
System.out.println(today.format(DateTimeFormatter.ISO_DATE);
using the ISO date format. You can define your own formatting pattern using a
DateTimeFormatter.ofPattern("dd.MM.yyyy");
for example.
You can use java.time.LocalDate.now() to get just the date.
Anyway, your case doesn't work as you expect because you are doing nothing to remove the time from the date: you are just "repressing" it, that's why it's zero. If you want to continue this way you could always substring it (substring the Date.toString() of course I meant).
Hope I helped.
java.util.Date's javadoc states:
The class Date represents a specific instant in time, with millisecond precision.
Thats why you have date with time
If you want a date you can use : java.time.LocalDate.now() (Java 8+)
First of all, stop using the old java.util.Date. The new Java 8 date and time API has much better classes for all date and time operations.
The LocalDate class does exactly what you want.
The current date can be obtained by LocalDate.now().
It also has a lot of facilities to add and subtract days, months etc. and it takes into consideration all the calendar special cases for you.
This question already has answers here:
Is java.sql.Timestamp timezone specific?
(7 answers)
Is java.util.Date using TimeZone?
(5 answers)
Closed 3 years ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Everything I've read says a Timestamp is UTC and has no offset or timezone. However, I'm 99% positive that the MS Sql Server JDBC is reading in the value from the DB and setting it in my local timezone.
Update: Please note the referenced possible duplicate question asks how to set a timestamp using UTC datetime. My question is how can I read the UTC timestamp value from a database.
The debugger shows the object held as a GregorianCalender object who's timezone is Denver.
This appears to solve the problem. I'm guessing when it returns a LocalDateTime it does not use the timezone.
LocalDateTime localDT = timestamp.toLocalDateTime();
odt = localDT.atOffset(ZoneOffset.UTC);
updated with suggestion from #Ole V.V.
This question already has answers here:
String to Date Conversion mm/dd/yy to YYYY-MM-DD in java [duplicate]
(5 answers)
Parsing from String to Date throws Unparsable Date Error
(3 answers)
Closed 4 years ago.
I'm very confused by the following behaviour. I am returning 2 dates as Strings from a method:
getLastSupplierFlightResults()
I have added a screenshot showing the returned dates as "2018-06-20 00:00:00" and "2018-06-24 00:00:00" respectively and left the debug trace in to show the values.
I simply want to convert the dates into 20180620 format.
The methods:
.withStartDate()
.withEndDate()
...accept String values
What I don't understand is where the date "Wed Dec 06 00:00:00 GMT 2017" is coming from? This is the value that ends up being passed into the .withStart and .withEnd methods as 20171206.
As always, there is probably a simpler way of achieving my aims.
You are using the format pattern string yyyyMMdd. You are parsing the date-time string 2018-06-20 00:00:00.
2018 matches yyyy. MM means that month should be two chars, so -0 is taken for the month. And -0 or just 0 is taken to be the month before month 1 of 2018, that is, December 2017. Finally 6 is taken as the day of month. It should have been two chars too, but since there is only one digit, SimpleDateFormat settles with that. The remainder of the string is tacitly ignored.
Exactly the same thing happens when parsing the other string.
It’s the long outdated SimpleDateFormat class in a nutshell: In its attempts to be friendly and helpful it produces the most unpleasant and confusing surprises. Where you would have wished it would tell you something is wrong, it just pretends that everything is fine. It’s one of the main reasons that this class is considered troublesome, and why the replacement for the old classes came out with Java 8 more than 4 years ago. So just never use SimpleDateFormat again.
Instead look into java.time and its DateTimeFormatter.
Also don’t get date values as strings from your database. Depending on the datatype that the query returns get either a LocalDateTime or a LocalDate object. This will free you completely from parsing.
Link: Oracle tutorial: Date Time explaining how to use java.time.
Seems like your pattern doesn't match stringDates you passing.
Try to use:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
This question already has answers here:
Generic support for ISO 8601 format in Java 6
(2 answers)
Closed 7 years ago.
I have the following strings which are coming from external server -
"2015-02-25T04:23:34.874-08:00", "2015-02-25T12:22:49.275Z"
I have to show these strings in my site along with the time zone which is available in the above strings.
Following is the format to show the date in my site -
"Feb 25, 2015 03:23 AM, GMT-08:00", "Feb 25, 2015 12:22 AM, GMT"
In JAVA 7 we have new pattern character 'X' to resolve this. We can parse both these values using the single pattern
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
But I am stuck with JAVA 6. What is the right pattern to use for JAVA 6 ?
The incoming date does not depict the timezone, still if you are sure that you would be getting all times in GMT, then you can simply format the incoming date using SimpleDateFormat and get you desired format.
You have an XSD dateTime on your hands which is notoriously hard to parse using standard java (this might have changed in java 8?)
The timezone is not compatible with the SimpleDateFormat parser nor is the random number of milliseconds (can be more than 3 per the spec).
I have not used it but I'm pretty sure Joda time has an easy solution for this. In case you're interested, here is some custom code I wrote a long time ago that parses them as well: https://github.com/nablex/types-base/blob/master/src/main/java/be/nabu/libs/types/utils/TimeFormat.java
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.format("2015-02-25T04:23:34.874-08:00")
The format is customizable.