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);
Related
This question already has answers here:
How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?
(2 answers)
Changing String date format
(4 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I am currently facing a problem when it comes to some Java methods that weren't explained to me in lectures very well. I need to write a program that accepts user-inputted strings (particularly a date) in yyyymmddhhss format, which should then convert to hh:mm Month day, year.
E.g. 201901151500 outputs: "03:00 PM January 15, 2019".
Currently, in my program, I have accepted the user's input and implemented a method that returns an error message if the inputted format is invalid.
Any tips on where to go from here? Advice is greatly appreciated-- thank you!
If you are using Java 8 you can use java.time API like so :
String input = "201901151500";
LocalDateTime dt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuuMMddHHmm"));
String output = dt.format(DateTimeFormatter.ofPattern("hh:mm a MMMM dd, uuuu"));
>> output = 03:00 PM janvier 15, 2019
Use the DateTimeFormatter as defined here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Pay attention to the parse method. You can define a formatter that takes in a string and then returns it in a certain way, almost any way you choose.
Here is the LocalDataTime class:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Example code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDateTime local = LocalDateTime.parse("2004 12 25", formatter);
This question already has answers here:
String to Date Conversion mm/dd/yy to YYYY-MM-DD in java [duplicate]
(5 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 4 years ago.
I got some values
"createTime": 1527217399000,
"updateTime": 1527218049000,
"createTime": 1527217399000,
"updateTime": 1527217954000,
But I can not parse them to date format, I'm not sure if there is something wrong with the data format,is there any method to parse them?
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Long tempTime = 1527217399000L;
Date createDate=format.parse(tempTime.toString());
System.out.println(createDate.toString());
Exception in thread "main" java.text.ParseException: Unparseable date:
"1527217399000"
at java.text.DateFormat.parse(DateFormat.java:366)
If you want to create a Date instance from a long value, use:
Date createDate=new Date(tempTime);
As the Javadoc mentions:
java.util.Date.Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) is meant to parse a String of the specified format into a Date (i.e. if you wanted to parse a String such as "2018-05-28 06:12:00" into a Date instance).
Since it's 2018, you really should be making use of the java.time API introduced in Java 8
long createTime = 1527217399000L;
LocalDateTime ldt = Instant.ofEpochMilli(createTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
System.out.println(format);
Which prints 2018-05-25 13:03:19 (on PC)
And if you don't have Java 8, there's the ThreeTen backport which makes the API available to earlier versions of Java
The old java.util.Date API is notoriously poor and if you want to make clear decisions about what type of date / time you're using - whether it's in a particular timezone, etc. - you should use the java.time API.
Assuming (you'd need to confirm) that these millisecond timestamps are in UTC timezone, you can use code like this to format it as a date/time in your local system timezone.
Long tempTime = 1527217399000L;
Instant instant = Instant.ofEpochMilli(tempTime);
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
This question already has answers here:
Java string to date conversion
(17 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 4 years ago.
I have a string e.g. Thu May 10 15:48:23 IST 2018. How to convert this string in the form of Calendar object with format 2018-05-10 15:48:23.84.
Start by using having a look at DateTimeFormatter and Parsing and Formatting for more details about how to parse and format date/time values in Java 8+
Based on your examples, something like...
String inValue = "Thu May 10 15:48:23 IST 2018";
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(inValue, inFormatter);
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS", Locale.ENGLISH);
String outValue = outFormatter.format(ldt);
System.out.println(outValue);
Will print 2018-05-10 15:48:23.00
Thank you for your response. I actually want the end result as Calendar Object with required format, not a string.
Calendar is effectively deprecated, you shouldn't be using it anymore. Even if you're not using Java 8+, you should be using the ThreeTen Backport API
Calendar (and Date and all other "date/time" class) are just containers of a value representing some point in time, they do not have any kind of "formatting" capabilities of their own. This is why the API has formatting classes. Keep you date/time values represented as appropriate classes until you need to display them, at that point, you should format the value to a String
This question already has answers here:
Convert String to java.util.Date
(4 answers)
Closed 7 years ago.
I read more questions on the web but I dont' find a solution yet.
I have a String like "14/05/1994", exactly in this format.
I need to covert it into java.util.Date in the same format.
I tried:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
But the result is: Sat May 14 00:00:00 CET 1994
It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?
A java.util.Date doesn't have a format. It's just a date.
When you print it out, e.g. using toString(), it uses a default format, which is what you're seeing. But you have that date.
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
I don't think that can be your code because DateFormat.format accepts a Date and returns a String, not the other way around. You might mean df.parse, which would get you the results you describe. But if you take your SimpleDateFormat and use its format method on the Date, then you should get back out 14/05/1994 as you want. A java.util.Date doesn't have a format, though.
This question already has answers here:
Date and time conversion to some other Timezone in java
(11 answers)
Closed 8 years ago.
I need Date strings sent to sencha frontend of the form:
'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
I'm unable to get the right text formatter string to output in this form.
Has anyone converted a Java Date object to this format ?
PS: not a duplicate of time lag while converting between timezones,
Its a query to get Dates in a specific format required for frontends using sencha which isn't answered by the "dd-MM-yyyy HH:mm:ss" format used in this post
This is the format pattern you need:
"EEE MMM dd yyyy HH:mm:ss zZ (zzzzzzz)"