Unparseable date: "2018-08-02T14:24:40.040353" - java

I am trying to parse a DateTime from C# to Java. Here is what comes as string from the java call "2018-08-02T14:24:40.040353". When I try to parse it, I get the following error Unparseable date: "2018-08-02T14:24:40.040353"
Here is the method for parsing the date
public static String dateFormater(String dateFromJSON, String
expectedFormat, String oldFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date date = null;
String convertedDate = null;
try {
date = dateFormat.parse(dateFromJSON);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat(expectedFormat);
convertedDate = simpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return convertedDate;
}

Your date look like a ISO_LOCAL_DATE_TIME, If you are using Java8 you can use java.time API and use the default format of LocalDateTime like this :
String dateString = "2018-08-02T14:24:40.040353";
LocalDateTime ldt = LocalDateTime.parse(dateString);
ldt.toString():
2018-08-02T14:24:40.040353

Related

Android: how to get the timezone string in the format specified here

How can I obtain the timezone format and return the string like so
https://msdn.microsoft.com/en-us/library/gg154758.aspx - column Time zone name,
for example "SA Pacific Standard Time"
or worse comes to worst, how can I obtain it like so: (UTC-05:00) Bogota, Lima, Quito, or at least (UTC-05:00)
so that I can manually match it to the former String if I put them all in a map?
Have you tried?
TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
Result be if your device happens to be in Australia
TimeZone GMT+09:30 Timezon id :: Australia/Darwin
This should return UTC as a String and Date object. You can change the date format.
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"
public static Date getUTCdatetimeAsDate()
{
//note: doesn't check for null
return stringDateToDate(GetUTCdatetimeAsString());
}
public static String getUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date stringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
This might be irrelevant (don't know if Android fully supports java-8), but you can do it using standard java api:
ZonedDateTime dateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Asia/Tokyo]");
System.out.println(dateTime); // 2007-12-03T10:15:30+09:00[Asia/Tokyo]
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("(O) z");
System.out.println(dateTime.format(formatter)); // (GMT+9) JST

Java: Converting String to Date

I am trying to get the current date in a Talend job and I am using this as my context variable:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
context.dateout = dateFormat.format(date);
System.out.println(context.dateout);
However, the type of the result is a String and not a Date.
How should I correct it?
Thank you very much!!
Try to do that according the following code:
String string = "2016-03-15";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date date = dateFormat.parse(string);
System.out.println(date);
} catch (ParseException ex) {
System.out.println(ex);
}
I don't know what your context.dateout means.
Note the difference between parse and format.
This is to create a string from a date:
dateFormat.format(date);
This is to create a date from a string:
dateFormat.parse(dateString);

How to convert java.util.Date with a given format to String instance?

I am new to Java and was trying to convert a String date to a certain java.util.Date format. The un-parsed String date is in yyyy-MM-dd HH:mm:ss pattern and I want to change it todd-MM-yyyy, for that I wrote some first implementation as follows below:
import java.text.SimpleDateFormat;
public class testDate {
public void parseDate() {
try {
String testDate = "2015-06-19 00:00:00.000000";
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date parsedDate = format1.parse(testDate);
System.err.println(parsedDate);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testDate td = new testDate();
td.parseDate();
}
}
The output Tue Dec 05 00:00:00 IST 24 is unexpected for me as a novice :).
So, I would request to suggest or point out where am I going wrong. Thanks
N.B:- There is a similar post (Convert java.util.Date to String) but it talks about parsing one Date format to another, but not String to a different Date Format
You're confusing parsing and rendering the date. You need to parse the date using the format it's in, of course, then you can render it using a different format.
String testDate = "2015-06-19 00:00:00.000000";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parsedDate = format1.parse(testDate);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
System.err.println(format2.format(parsedDate));
The SimpleDateFormat should have the right pattern to parse the given string. If you construct format1 like this
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
the "parse" call will work correctly on the given date. After that, you are able to format it, to your own needs:
public void parseDate() {
try {
String testDate = "2015-06-19 00:00:00.000000";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date parsedDate = format1.parse(testDate);
//change the pattern
format1.applyPattern("dd-MM-yyyy");
System.err.println(format1.format(parsedDate));
}
catch (Exception e) {
e.printStackTrace();
}
}
Your String does not seem to be in the same format as your SimpleDateFormat.
try
String testDate = "19-06-2015";
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
Date date;
try {
date = format1.parse(testDate);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You're not parsing the input date correctly. format1 is supposed to be the format of the incoming date, not what you want the date to be.
try {
String testDate = "2015-06-19 00:00:00.000000";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parsedDate = format1.parse(testDate);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
System.err.println(format2.format(parsedDate));
}
When you parse the string you want a Date object that is a representation of your original date. Therefore, you need to parse it with the format it comes with. You then need another SimpleFormat with your target format for the output.
So, try this:
import java.text.SimpleDateFormat;
public class testDate {
public void parseDate() {
try {
String testDate = "2015-06-19 00:00:00.000000";
// not sure about the Millisecond part ... might be SSSSSS
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date parsedDate = format1.parse(testDate);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
String ouput = format2.format(parsedDate)
System.err.println(parsedDate);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testDate td = new testDate();
td.parseDate();
}
}

Java convert date to sql timestamp

I'm trying to convert a date (string) extracted from a csv file, convert it to sql timestamp and upload using prepared statement. What I have is:
String test = "8/10/2014 16:59";
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
toFormat.setLenient(false);
Date date2 = null;
try {
date2 = toFormat.parse(test);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf(date2);
//java.sql.Timestamp sqlDate2 = new java.sql.Timestamp(timestamp);
//sql_statement.setTimestamp(1, ts2);
As you can see my code is messy as I'm trying to solve this problem. I'm always getting an error in eclipse:
java.text.ParseException: Unparseable date: "8/10/2014 16:59"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:555)
at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)
at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)
at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)
at java.lang.Thread.run(Thread.java:745)
I even try using:
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
String yourformattedDate = sdf.format(test);
and diff error shows up"
Exception in thread "Thread-6" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:301)
at java.text.Format.format(Format.java:157)
at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:562)
at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)
at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)
at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)
at java.lang.Thread.run(Thread.java:745)
Anyone can help on why the date is unparseable? and how to convert it to a proper sql timestamp? thank you
Your fromFormat format specifier is
dd/MM/yyyy hh:mm
but should be
dd/MM/yyyy HH:mm
And change the toFormat to yyyy-MM-dd HH:mm:ss.SSSSSS
Then your parse code should change from
date2 = toFormat.parse(test);
to
date2 = fromFormat.parse(test);
System.out.println(toFormat.format(date2));
And I get the output
2014-10-08 04:59:00.000000
Please use following code it will serve your need
String test = "8/10/2014 16:59";
Date date2 = null;
SimpleDateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
try
{
date2 = fromFormat.parse(test);
Timestamp tt = new Timestamp(date2.getTime());
System.out.println(tt);
} catch (ParseException ex)
{
date2 = null;
}
When you parse a date-string that looks like this:
String test = "8/10/2014 16:59";
you're using 24-hours format (16:59) you should use HH instead of hh.
See the following code snippet:
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
fromFormat.setLenient(false);
Date date2 = null;
try {
date2 = fromFormat.parse(test);
System.out.println("date2 = " + date2); // prints date2 = Wed Oct 08 16:59:00 PDT 2014
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So you have a String, and you want to parse it as a Date... Let's see if this example helps you:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateConverter
{
public static void main(String[] args) {
String test = "8/10/2014 16:59";
SimpleDateFormat sdf1 = new SimpleDateFormat("d/MM/yyyy HH:mm"),
sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*
'sdf1' will be used to parse your input string as a date
'sdf2' will be used to output a string with your desired date format
*/
Date d;
String formatted_date;
try {
// Parse the string to a date, using the defined format
d = sdf1.parse(test);
// Now, format the date with 'sdf2' and store it in a string
formatted_date = sdf2.format(d);
System.out.println(formatted_date); // The output is: 2014-10-08 16:59:00
} catch(ParseException e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}
Was able to fix it using the code below:
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date2 = null;
String def = perRow[cnt].replaceAll("8", "08");
try {
date2 = fromFormat.parse(def);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long tsTime1 = date2.getTime();
java.sql.Timestamp sqlDate2 = new java.sql.Timestamp(tsTime1);
sql_statement.setTimestamp(2, sqlDate2);
Of course, I don't know if this is the correct or proer way to it as:
1. replace the sting with correct day 'dd'
2. parse to date format
3. convert to long
4. convert to sql date
Anyone knows a better way or idea, thread is open for comments. thank you.
You can convert it using java.sql.Timestamp. Here is a snippet:
String strDate = "15/07/1989 15:30";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date date = format.parse(strDate);
System.out.println(date);
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println(timestamp);
And the output will be:
Sat Jul 15 15:30:00 IST 1989
1989-07-15 15:30:00.0
Simple!
First parse fromDate then format in toDate pattern.
toFormat.format(fromFormat.parse(test));

Java: Issue in SimpleDateFormat class

I am trying to convert a date string of format dd.MM.YYYY into date object as following:
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.YYYY");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
But I am getting this result Date is : Sun Dec 26 00:00:00 MST 2010
I tried it in different ides and even on compileonline.com , still same result.
So Am I doing anything wrong here, because its not suppose to behave like this.
Please help.
The pattern for year is incorrect. You need to say:
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
The correct representation is yyyy
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = null;
try {
date = (Date)formatter.parse(start_dt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("Date is : "+date);
public static void main(String[] args) {
try {
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
} catch (ParseException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
you should learn about DateFormat from here link1 and link2, you will realize that your code should be like that (year should be written in small letters).
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);

Categories

Resources