java.lang.IllegalArgumentException: Cannot format given Object as a Date - java

I recive a date in this format
2014-12-09 02:18:38
which i need to convert it to
09-12-2014 02:18:38
I tried converting this way
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
String input = "2014-12-09 02:18:38";
String strDate = sdf.format(input);
System.out.println(strDate);
}
}
But i am getting this exception during Runtime
Exception in thread "main" 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 TestDate.main(TestDate.java:15)
Could anybody please help me how to resolve this .

Try this
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String input = "2014-12-09 02:18:38";
Date date = sdfIn.parse(input);
System.out.println(sdfOut.format(date));
}
Also, note that m is for minutes, while M is for months.

Instead of
String strDate = sdf.format(input);
use
String strDate = sdf.parse(input);
Also see this question

SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
// String to Date:
String input = "2014-12-09 02:18:38";
Date date = sdf.parse(input);
// Date to String:
String strDate = sdf.format(date);
System.out.println(strDate);

SimpleDateFormat in can be used to format a Date object.
First you have to parse your string to a Data object using format on the SimpleDateFormat you declared.
After that you can output it using a second SimpleDataFromat to a string you want.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String input = "2014-12-09 02:18:38";
Date dt = sdf.parse(input);
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String strDate = sdf2.format(dt);
System.out.println(strDate);

Related

Change a Java date-time string to date

Well I tried to look for many questions but couldn't find something relevant. I have a string that has the following data:
String sDate = "2018-01-17 00:00:00";
This comes from an application, and I need to convert it into the following Date format
17-01-2018
I went through this link but could not relate.
Can someone help..?
If you are using Java 8, you can use java.time library and :
String sDate = "2018-01-17 00:00:00";
//Step one : convert the String to LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(sDate, formatter);
//Step two : format the result date to dd-MM-yyyy
String result = date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
Output
17-01-2018
Another Overengineering solution (It works just in your case) you can benefits from the default format of LocalDateTime :
String result = LocalDateTime.parse(sDate.replace(" ", "T"))
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
public static void main(String args[]) {
String sDate = "2018-01-17 00:00:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = df.parse(sDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df1.format(date));
}
it should solve your problem.
You need to use the SimpleDateFormat:
String sDate = "2018-01-17 00:00:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(sDate);
SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df1.format(date));
Go through the SimpleDateFormat class to get into details for this

Converting of string type to date parse not working

I am converting the string type to date and parsing it to a HH:mm format but while formatting its not working throwing exception,campusconfig.getworktime() is date Below is my code
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
if ((from1 != null)) {
if(campusConfig.getWorkFromTime()!=null){
String time = "";
Date timech;
time = campusConfig.getWorkFromTime().toString();
timech = dateFormat.parse(time);
if(from1.before(timech)){
String errormsg = message.getString("message.reports.time.select.campustime");
errorMessageUI(errormsg);
}
If campusConfig contains a full date (date and time) you need to parse it with SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSSSSS") (with the correct format) then you can trasform the date in only time with other SimpleDateFormat("HH:mm") format(date).
Sample:
public class DateFormat {
public static void main(String[] args) throws ParseException {
String campusConfig = "2015-11-26 11:46:20";
SimpleDateFormat sdf_in = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf_in.parse(campusConfig);
SimpleDateFormat sdf_out = new SimpleDateFormat("HH:mm");
System.out.println("Only Time : " + sdf_out.format(d));
}
}
try something like below,
new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(campusConfig.getWorkFromTime().toString()));
try this
java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");

how to format date using SimpleDateFormat

I cannot format a date.
dateFormat.format() accepts a Date as argument. So I created a new Date()
It says the below Date() method is deprecated, and I get the below exception while running.
exception:
Exception in thread "main" java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:598)
public class MyDate {
public static void main(String[] args) {
Date date = new Date("2012-02-16T00:00:00.000-0500");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate); // how do I test this conversion??
}
}
My database has date of the format - 2012-02-16T00:00:00.000-0500
I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss
I'm using Java6
Thanks to #Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet
Complete Solution:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SampleDate {
public static void main(String[] args) throws ParseException {
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
String strDate = parseFormat.format(date);
System.out.println(strDate);
// if you get date of type 'java.sql.Date' directly from database cursor like
//rs.getDate("created_date"), just pass it directly to format()
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate);
}
}
/*
Output:
2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00
*/
you can also convert java.util.Date to java.sql.Date like this,
String dateString = "03-11-2012";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = dateFormat.parse(dateString);
java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");
Along with the rest of your code this writes:
16-Feb-2012 05:00:00
The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.
This answer assumes Java 7, or you would be using the new date & time API from Java 8

how to convert string into date? JAVA

I have this string: 7 -Jun- 2014.
I want to convert to java.utils.Date;
I use this Code
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
but I get this exception :
java.text.ParseException: Unparseable date: "7-Jun-2013"
at java.text.DateFormat.parse(Unknown Source)
at ma.abcsolution.util.Test.main(Test.java:15)
try using
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
the SimpleDateFormat look at the given string like it you tell it to, so in your example it look for a string with 2 chars for days, followed by a '/' sign and then 2 chars for month and so on
Using SimpleDateFormatter you can convert from date string to date and date to date String
public final class DateUtil {
private static final String DEFAULT_DATE_FORMAT = "dd-MMM-yyyy";
private DateUtil() {
}
public static final String formatDate(final Date date, final String dateFormat) {
DateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
return format.format(date);
}
public static final Date formatDate(final String date) throws ParseException {
return formatDate(date, DEFAULT_DATE_FORMAT);
}
public static final Date formatDate(final String date, final String dateFormat) throws ParseException {
DateFormat format = new SimpleDateFormat(dateFormat);
return format.parse(date);
}
}
Whenever you are going yo format date please verify the format you are using
In your case you used dd/MM/yyyy but date you used in format dd-MMM-yyyy.
Use
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
to format the date. You used the wrong format with the / signs.
See also:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Date date = new SimpleDateFormat("d-MMM-yyyy").parse("7-Jun-2014");
Use this code. It is simple.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
public static void main(String args[]){
SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
Date date=new Date(dateInString);
System.out.println(ft.format(date));
}
}

Unparseable date exception when converting a string to "yyyy-MM-dd HH:mm:ss"

Hi I am trying to convert a string 19611015 to a date formate of ("yyyy-MM-dd HH:mm:ss") before storing it into a sybase database table. I have tried the below code which gives me the error:
Unparseable date: "19611015"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.parse("19611015"));
I have been reading some long and complex solutions to this, some suggesting to use Locale. Could someone explain maybe an alternative simple solution to convert string I have to a date format I am after above. Thank you.
The date in string is in yyyyMMdd format and want to convert it into yyyy-MM-dd HH:mm:ss so use below code :
DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = originalFormat.parse("19611015");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
Output :
1961-10-15 00:00:00
Achilles, below code might solve your problem:
package com.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LongToDate {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(19611015 * 1000);
String dateFormatString = "yyyy-MM-dd HH:mm:ss";
DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
String result = dateFormat.format(cal.getTime());
System.out.println(result);
//Output: 1969-12-10 15:46:18
}
}

Categories