Unparseable date while converting to timestamp - java

Can anyone please clear me, what does this exception mean:
Exception in thread "main" java.text.ParseException: Unparseable date: "06.10.2013 00:00".
That was found such a date in file 06.10.2013 that cannot be parsed? must it be always 06/10/2013? I got it, when i convert String[] to Timestamp while reading file. But I have two formats in a file like 03/10/2013 and 03.10.2013, which must I use then, instead of this:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String[] temp = line.split(",");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy HH:mm" );
java.util.Date parsedDate = dateFormat.parse(temp[0]);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
parsedDate.getTime());
for (int i = 1; i < temp.length; i++) {
o.setTimestamp(timestamp);
i have default both of formats in my file 06.10.2013 15:00:00 and 06/10/2013 15:00:00

Perform an analysis on the String before naively try to parse it with your current SimpleDateFormat:
SimpleDateFormat dateFormat;
//check if the string to parse contains a dot
if (stringContainingTimestamp.contains(".")) {
dateFormat = new SimpleDateFormat("MM.dd.yyyy HH:mm");
} else {
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
}
//rest of the code...
Another option may be just replacing the dot char by slash using String#replace(char, char):
stringContainingTimestamp = stringContainingTimestamp.replace('.', '/');
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//rest of the code...
The choice is yours.

Related

change date format DD-MON-YYYY to DD/MM/YYYY

I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .
Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"
I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response

Error converting Date from one format to another format

Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.

Convert String [] to timestamp [duplicate]

This question already has answers here:
Java converting a Date to a different format
(2 answers)
Closed 8 years ago.
how to convert string [] temp to Timestamp:
String[] temp = line.split(",");
for (int i = 1; i < temp.length; i++) {
objekt.setTimestamp(timestamp);}
if i use
Timestamp ts= Timestamp.valueOf(temp[0]);
objekt.setTimestamp(ts);}
i get this exceptionTimestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
if i use this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(temp[0]);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
i get
" java.text.ParseException: Unparseable date: "06/11/2013 22:00"
thank you everyone for suggestions, i can read the timestamp but i must to read this format month-day-year: 06/18/2013 21:00:00, and with SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm"); i get 2013-06-18 18:00:00, if i change MM or dd or yyyy hier "MM/dd/yyyy HH:mm" i can't even get the month first then date and year
Your date format must actually match the text being provided. '/' and '-' do not match.
For the string you have provided, it appear the format might be "MM/dd/yyyy HH:mm".
It could also be "dd/MM/yyyy HH:mm", as it is not clear whether that date is June 11 or November 6.
Use the below code:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Assuming that 06 in the string Unparseable date: "06/11/2013 22:00" correponds to month
On your 2nd code block where you got java.text.ParseException: Unparseable date: "06/11/2013 22:00, your SimpleDateFormat pattern is wrong as your String is "06/11/2013 22:00", So you have to use pattern :
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
For more details, see java.text.SimpleDateFormat
You can do something like this :
Timestamp ts= null;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String[] temp = {..,..}; // it's string time array
for (int i = 1; i < temp.length; i++) {
Date convertedDate = dateFormat.parse(temp[i]);
ts = new Timestamp(convertedDate.getTime());
System.out.println(ts);
}

Get time from entire date

I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));

change date format in java

I have string in 'MM/dd/yyyy' format and want to convert it into 'dd-MM-yy'.
e.g. '04/01/2012' should be converted into '01-Apr-12'
Can anyone please suggest how to get this?
SimpleDateFormat currentFormat = new SimpleDateFormat("mm/dd/yyyy");
SimpleDateFormat newFormat = new SimpleDateFormat("dd-mm-yy");
String dateInOldFormat="04/01/2012";
Date temp = currentFormat.parse(dateInOldFormat);
String dateInNewFormat= newFormat.format(temp);
i think things are pretty simple from here on...
You can use the Date class in Java. Parse a string with your format, and then output using a different format.
Below is example of date conversion... For your program, do changes accordingly...
String dateStr = "Thu Jan 19 2012 01:00 PM";
DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse( dateStr );
} catch ( ParseException e ) {
e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format( date );
}
System.out.println(formattedDate);
Output is 2012-01-19 13:00:00
Good Luck

Categories

Resources