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));
Related
My string date --> 2016-10-02T00:00:00.000Z. I want to get only date from this string. I tried to parse through below coding but it throws me error! I have exactly the same format as mentioned in the string. Any answers?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
try {
Date myDate = sdf.parse( dateofJoining.replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
System.out.println("Date only"+ myDate );
} catch (ParseException e) {
e.printStackTrace();
}
I also tired below code,
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error which i get
java.text.ParseException: Unparseable date: "2016-10-02T00:00:00.000Z" (at offset 19)
05-12 00:18:36.613 4330-4330/com.vroom.riderb2b W/System.err: at java.text.DateFormat.parse
change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ
in your code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date date = format.parse(dtStart.replaceAll("Z$", "+0000"));
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you want to get date/mm/yy from it:
use:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.i("DATE", sdf.format(date)); //previous date object parsed
if you want output format: hour:minute AM/PM
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(date));
EDIT
More easier option is to split the string in two parts like:
String dateString = "2016-10-02T00:00:00.000Z";
String[] separated = dateString.split("T");
separated[0]; // this will contain "2016-10-02"
separated[1]; // this will contain "00:00:00.000Z"
try with this :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Try
Calendar calendar = Calendar.getInstance();
TimeZone localTZ = calendar.getTimeZone();
String format1 = "yyyy-MM-dd"; //will return 2017-01-31
String format2 = "dd"; //will return DAY only like 31
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTZ );
String result = sdf.format(your_date);
For me it's worked like this:
textView_last_comm.setText(parseDateFormat(passDetailsModel.getLast_comm(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "dd/MM/yy HH:mm"));
public static String parseDateFormat(String dateToFormat, String inputFormat, String outputFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormat);
Date date = null;
String str = null;
try {
date = inputFormat.parse(dateToFormat);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
You can get date easily by using String.substring() method:
String string = "2016-10-02T00:00:00.000Z";
String date = string.substring(0, 10);
Log.d("SUCCESS", "DATE: " + date);
OUTPUT:
D/SUCCESS: DATE: 2016-10-02
This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 6 years ago.
I want to convert timestamp to date in android, I tried this code but not get expected result.
My Code Is.
String timeStampST = "2016-12-13 09:47:11";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(timeStampST);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Out Put
Tue Dec 13 16:15:02 GMT+05:30 2016
Expected Out Put
Tue Dec 13 2016
I need day in my output
Kindly copy paste this answer. It will help you.
String timeStampST = "2016-12-13 09:47:11";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
String resultDate = null;
try {
date = fmt.parse(timeStampST);
SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy");
resultDate = fmtOut.format(date);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OUTPUT
resultDate = 12/13/2016
Use this method with string and input and output format.
public static String parseDateToddMMyyyy(String time, String inputPattern, String outputPattern) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date;
String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
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);
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);
I have a date in(string) in dd-mon-yyyy format and I want to compare this date with system date.
eg.
I have 12-OCT-2010
and I want to compere this with system date in same format
You can use the SystemDateFormat class to parse your String, for example
final DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
final Date input = fmt.parse("12-OCT-2010");
if (input.before(new Date()) {
// do something
}
Note that SimpleDateFormat is not threadsafe, so needs to be wrapped in a ThreadLocal if you have more than one thread accessing your code.
You may also be interested in Joda, which provides a better date API
Use SimpleDateFormat http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
String d = "12-OCT-2010";
try {
Date formatted = f.parse(d);
Date sysDate = new Date();
System.out.println(formatted);
System.out.println(sysDate);
if(formatted.before(sysDate)){
System.out.println("Formatted Date is older");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I would recommend using Joda Time. You can parse that String into a LocalDate object very simply, and then construct another LocalDate from the system clock. You can then compare these dates.
Using simpledateformat -
String df = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(df);
Calendar cal = Calendar.getInstance();
/* system date */
String systemdate = sdf.format(cal.getTime());
/* the date you want to compare in string format */
String yourdate = "12-Oct-2010";
Date ydate = null;
try {
ydate = sdf.parse(yourdate);
} catch (ParseException e) {
e.printStackTrace();
}
yourdate = sdf.format(ydate);
System.out.println(systemdate.equals(yourdate) ? "true" : "false");