Java SimpleDateFormat: Unparseable Date exception - java

The code is as mentioned below:
public static void main(String[] args){
Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");
try{
Date formattedDate = dateFormat.parse(date.toString());
System.out.println(formattedDate.toString());
}catch(ParseException parseEx){
parseEx.printStackTrace();
}
}
In code above, dateFormat.parse(date.toString()) is throwing unparseable date exception: Unparseable date: "Mon Jan 28 18:53:24 IST 2013
I am not able to figure out the reason for it.

Format the java.util.Date instance into String using SimpleDateFormat.format(java.util.Date)
Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");
try {
Date formattedDate = dateFormat.parse(dateFormat.format(date));
System.out.println(formattedDate.toString());
} catch (ParseException parseEx) {
parseEx.printStackTrace();
}

Why would you want to convert a date to a string and parse it back to a date?
The reason your code fails is because you are trying to convert a full date with a formatter which only accepts dates in the dd-MMM-yyy-format.

public static void main(String[] args) throws ParseException {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM d HH:mm:ss Z yyyy");
Date formattedDate = dateFormat.parse(date.toString());
System.out.println(formattedDate);
}
This is what you exactly want to do ...yes?

Related

Why is the date unparseable [duplicate]

This question already has answers here:
java.text.ParseException: Unparseable date
(10 answers)
Closed 3 years ago.
I'm attempting to parse a new Date object, but I keep hitting the following error.
W/System.err: java.text.ParseException: Unparseable date: "Thu May 16 09:28:39 GMT+01:00 2019"
I've attempted various different patterns for dateFormat, but nothing seems to work.
This is where the error is.
c.setTime(dateFormat.parse(oldDate));
Code
public static String addDay(int numberOfDays) {
String oldDate = String.valueOf(new Date());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
try {
c.setTime(dateFormat.parse(oldDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}
Try This function
In your question you are converting Date to string
then after you are once again Parsing String to Date
that is very Long way. you can directly set as
c.setTime(oldDate);
public static String addDay(int numberOfDays) {
Date oldDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
c.setTime(oldDate);
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}
The pattern should be like :
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Your pattern is wrong. You should use EEE MMM dd HH:mm:ss z yyyy

How change String to Date format

I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);

How to parse given string format to dd-MMM-yyyy hh:mm:ss:aa?

I can get the output as Wed May 11 15:36:08 IST 2016, but how do I convert the date to a string with the required format?
Required format is: 12-05-2016 16:05:08 pm
What I tried is,
public class Test {
public static void main(String args[]) throws ParseException{
String epoche="1462961108000";
Long initialLogTime = Long.valueOf(epoche);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(initialLogTime);
Calendar fromDateTime = calendar;
Calendar toDateTime = fromDateTime;
toDateTime.add(Calendar.MINUTE, 30);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss:aa");
String datestring = String.valueOf(fromDateTime.getTime());
String datestring1 = String.valueOf(toDateTime.getTime());
System.out.println(datestring); //here output is Wed May 11 15:36:08 IST 2016
System.out.println(datestring1); // here output is Wed May 11 15:36:08 IST 2016
Date dates = dateFormat.parse(datestring);
Date date1s = dateFormat.parse(datestring1);
System.out.println(dates);
System.out.println(date1s);
}
}
The error I am getting is:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed May 11 16:05:08 IST 2016"
at java.text.DateFormat.parse(DateFormat.java:357)
at test.Test.main(Test.java:27)
You need to format your dates accordingly. This shall help you
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
System.out.println(dateFormat.format(fromDateTime.getTime()));
System.out.println(dateFormat.format(toDateTime.getTime()));
if you are using java 8, you can use
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss:aa");
System.out.println(date.format(formatter));
Please try this
public static void main(String[] args) {
String epoche = "1462961108000";
Date date = new Date(Long.parseLong(epoche));
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
String strDate = sdf.format(date);
System.out.println(strDate);
}
Output
11-05-2016 15:35:08:PM
In Android, Pass String Like 11/10/2017 11:16:46 to function ConvertDateTime
public String ConvertUpdate(String strDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date d = simpleDateFormat.parse(strDate);
simpleDateFormat = new SimpleDateFormat("dd MMM yy hh:mm a");
return simpleDateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
output
10 Nov 17 11:16 AM

java - Date Timezone Conversion From String to Date to String

I have a routine which receives a date in the format yyyy, MMM dd, HH:mmin GMT time. I have to convert this String to a Date object and I do it with a SimpleDateFormat, but now I have to take that Date object and format it in GMT-5 using again a SimpleDateFormat, but the method is returning the same original String Date. Why? This is my routine:
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
#Test
public void testDateConversion() {
String strDate = "2015, Aug 03, 23:50";
Date date = DateFormatter.parseDate(strDate, "yyyy, MMM dd, HH:mm");
String dateFormatted = DateFormatter.formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Error message:
org.junit.ComparisonFailure:
Expected :2015, Aug 03, 19:50
Actual :2015, Aug 03, 23:50
Solved by indicating the Timezone of the receiving date string:
public static TimeZone originTimeZone = TimeZone.getTimeZone("GMT"); // +Added
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(originTimeZone);// +Added
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
Try changing the String you pass in to format, like so
#Test
public void testDateConversion() {
//also changed this from 23:50 to 19:50
String strDate = "Aug 03, 2015, 19:50";
Date date = parseDate(strDate, "MMM dd, yyyy, HH:mm");
String dateFormatted = formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Otherwise your code is formatting as expected. The problem is, you're passing the date string in pre-formatted, so your end isn't going to change from what you started with. Your unit test is failing because you're passing in "Aug 03, 2015, 23:50" and telling Junit to expect "Aug 03, 2015, 19:50";. When you pass in a String to Date like that, the time value isn't going to change at all

JXDatePicker:unpearsable date

I'm developing a small application with java swing and I'm stuck with the JXDatePicker
I need to convert the date from the datepicker so I can insert it in mysql DB
here is the code
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date invoiceDate = formatDate.parse(jXDatePicker3.getDate().toString());
java.sql.Date sqlDate = new java.sql.Date(Long.parseLong(dateFormat.format(invoiceDate)));
and this is the error
java.text.ParseException: Unparseable date: "Thu Dec 19 00:00:00 GMT+01:00 2013"
There is no need for dateFormat. Once the date is parsed you can simply use getTime() to return the long value to be used in the constructor.
public class DateTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date invoiceDate = formatDate.parse("Thu Dec 19 00:00:00 GMT+01:00 2013");
//Once you have a date use .getTime() to get Long value.
java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());
}
}
Let's start with the fact that dateFormat.format will return a String...
You could be trying something more like..
java.sql.Date sqlDate = new java.sql.Date(
formatDate.parse(
dateFormat.format(invoiceDate)).getTime());
Instead, or may be even...
java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());

Categories