I am trying to convert date from one format to other format but the following code is giving me the exception: please help
public class Formatter {
public static void main(String args[]) {
String date = "12-10-2012";
try {
Date formattedDate = parseDate(date, "MM/dd/yyyy");
System.out.println(formattedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Date parseDate(String date, String format)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(date);
}
}
To convert from "MM-dd-yyyy" to "MM/dd/yyyy" you have to do as follows:
SimpleDateFormat format1 = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("MM/dd/yyyy");
Date date = format1.parse("12-10-2012");
System.out.println(format2.format(date));
If you input "12-10-2012" then output will be "12/10/2012":
change slash / to dash -
MM-dd-yyyy instead of MM/dd/yyyy
it should be Date formattedDate = parseDate(date, "MM-dd-yyyy");
Your format uses slashes (/) but the date you supply uses dashes (-). Change to:
Date formattedDate = parseDate(date, "MM-dd-yyyy");
And you should be good :)
Try this.
Date formattedDate = parseDate(date, "MM-dd-yyyy");
Related
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 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();
}
}
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));
I want to convert the string 11-7-2013 10:51:10 to a Date object.
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
I have the following code, But I'm getting a ParseException.
Try
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Use
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
In SimpleDateFormat the string passed to constructor is the same representation of date format that date is going to parse by this formatter.
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
instanse of
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
You need to use "-" instead of "/" in your date format as your date string contains "-" ,
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
There are two ways here
"dd/MM/yyyy hh:mm:ss" with String Date ="11/7/2013 10:51:10";
or
"dd-MM-yyyy hh:mm:ss" with String Date="11-7-2013 10:51:10";
please refer the below format
tring string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
I have a string in "dd-MM-yyyy HH:mm" and need to convert it to a date object in the format
"yyyy-MM-dd HH:mm".
Below is the code I'm using to convert
oldScheduledDate = "16-05-2011 02:00:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = (Date)formatter.parse(oldScheduledDate);
Now when I print oldDate, i get
Sat Nov 01 02:00:00 GMT 21, which is completely wrong, what am I doing wrong here?
String dateSample = "10-01-2010 21:10:05";
String oldFormat = "dd-MM-yyyy HH:mm:ss";
String newFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
try {
System.out.println(sdf2.format(sdf1.parse(dateSample)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
"yyyy-MM-dd" doesn't even look the same as "16-05-2011". Hmm. Well, why not?
Hints:
DateFormat is very literal. It takes the format specified and uses it -- nothing fancy.
Process: Input Date String -> Convert(With Input Format) -> Date -> Convert(With Output Format) -> Output Date String
The code in the post contains an Output Format, but no Import Format.
A simple approach is to swap around the letters.
String s = "16-05-2011 02:00:00";
String newDate=s.substring(6,10)+s.substring(3,6)+'-'+s.substring(0,2)+s.substring(10);
you need to use formatter when you want to output formatted date
public static void main(String[] args) throws ParseException {
String oldScheduledDate = "16-05-2011 02:00:00";
DateFormat oldFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = (Date)oldFormatter .parse(oldScheduledDate);
System.out.println(formatter.format(oldDate));
}