This question already has answers here:
java.text.ParseException: Unparseable date
(10 answers)
Closed 8 years ago.
I am trying to convert a String ("01-OCT-2014") into Date format.
Below is my code for this.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date d= formatter.parse("01-OCT-2014");
System.out.println("Converted date:"+d);
The above code is working fine in Windows.
But when I am running in Unix environment, it is throwing an exception Unparseable date
Can any one help me out in solving this issue..
I suspect the problem is simply that your Unix environment isn't running in an English locale - so when it tries to parse the month name, it's not recognizing "OCT" as a valid value.
I would suggest using code like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date d = formatter.parse("01-OCT-2014");
System.out.println("Converted date: " + d);
You might also want to specify the time zone of the formatter - if you're just parsing a date, then using UTC would make sense. Note that your output will use the system local time zone, because you're using Date.toString() - that doesn't mean that the Date has any notion of the time zone in its data; a Date is just a point in time.
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:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I am getting an epoch String from my DB which looks something like this : 1391328000000
I am having a hard time trying to convert it to Java Date.
I tried the following :
private String buildDate(String dateString){
System.out.println("dateString " + dateString);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(Integer.parseInt(dateString));
return formatted;
}
I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:
Date d = new Date(Long.parseLong(dateString));
You need to turn it into a java.util.Date object in order for SimpleDateFormat to handle it. Also, a value like what you quoted needs to be parsed as a long, as it is too large for an int.
That is, change the line where you set formatted to be:
String formatted = format.format(new Date(Long.parseLong(dateString)));
As an aside, if the project you're working on can handle an extra external dependency, switch date/time handling over to the joda library. The stuff in java.util (that is, Date and Calendar) rapidly becomes painful and error-prone to work with.
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.
I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.
Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
.atZone(ZoneId.of("GMT-4"))
.format(formatter);
System.out.println(formattedDtm); // => '2013-06-27 09:31:00'
I thought this might be useful for people who are using Java 8.
You need to convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
This question already has answers here:
Getting device's local timezone
(4 answers)
Closed 9 years ago.
In my app i want to get the time zone of device and then use it in simple date format, here is my code:
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") ;
sdf.setTimeZone(tz);
i am getting correct id and display name from tz but the code does not works correctly for time.
you should use TimeZone.getDefault()
it returns a TimeZone based on the time zone where the program is running.
You are setting the TimeZone of the SimpleDateFormat so that it prints out a time in your local timezone, but the 'Z' at the end prints out a literal letter Z, which means UTC. You should remove the quotes around it so that it prints the actual timezone at the end, e.g. +0100.
That said, you haven't mentioned whether you're using the SimpleDateFormat for printing or for parsing.
I have a timestamp field of Oracle in Java String
String strDate = "24.12.12 03:30:00,000"; //Timestamp field of Oracle
I need to convert it to Java.util.Date.
Please tell me a way to do this.
Note: I don't have first two digits for the year, how can java understand that this is year 1912 or 2012 and so on.
The class you want is SimpleDateFormat. You'll create a pattern that matches your expected input, and parse it. Here's an example, though it may be incorrect in some places (I used 0-23 hour, maybe you want 1-24, etc.)
String strDate = "24.12.12 03:30:00,000";
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy HH:mm:ss,SSS");
Date date = sdf.parse(strDate);
Regarding your 1912 vs 2012 problem, Java will make some assumptions when there are only 2 digits. Specifically:
For parsing with the abbreviated year pattern ("y" or "yy"),
SimpleDateFormat must interpret the abbreviated year relative to some
century. It does this by adjusting dates to be within 80 years before
and 20 years after the time the SimpleDateFormat instance is created.