I am reading a date column from an Excel Sheet in my Java program via Apache POI that returns the string Fri Jan 16 00:00:00 EST 2015. I need to format this string to 01/16/2016.
I try the following using the SimpleDateFormat to parse the String to a Date Object, then back to the formatted String I need
String inputDate = excelData.get(20).toString(); // Returns Fri Jan 16 00:00:00 EST 201
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(inputDate);
String outputDate = new SimpleDateFormat("MM/dd/yyyy").format(date);
However the following is returned when trying to parse inputDate
Unparseable date: "Fri Jan 16 00:00:00 EST 2015"
Is my inputDate unreadable or am I missing something here? I've also thought of formatting the cell itself to the proper date format instead - thoughts?
Your problem is on this line:
Date date = new SimpleDateFormat(**"MM/dd/yyyy"**).parse(inputDate);
The date format you've described looks more like:
"DDD MMM DD HH:mm:ss tz yyyy" or something like that.
Use https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html to find the right formatter in order to read the date.
The output looks fine, given you can read the input string format as the right format into a Date.
Your code tells SimpleDateFormat to use MM/dd/yyyy format to PARSE the date, so of course it can't work, since this is not the format of the string you get.
The POI FAQ at https://poi.apache.org/faq.html#faq-N1008D gives a snippet of code to read a date:
case HSSFCell.CELL_TYPE_NUMERIC:
double d = cell.getNumericCellValue();
// test if a date!
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// format in form of M/D/YY
cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
cellText = cal.get(Calendar.MONTH)+1 + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" +
cellText;
}
Have you tried that?
Change
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(inputDate);
to
Date date = new SimpleDateFormat("EEE MMM DD HH:mm:ss zzz yyyy").parse(inputDate);
zzz Time zone EST
EEE Day name in week Tuesday
MMM Month in year July
Reference: SimpleDateFormat doc
Related
I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd
Currently using Parse to obtain a date on an object by using:
Date date = object.getCreatedAt();
The returned String when displaying it in a TextView is this:
Mon Mar 17 22:39:27 CET 2014
However I really only want the MM/DD/YYYY to display like so: 3/17/2014
I've tried this:
Date date = object.getCreatedAt();
SimpleDateFormat originalFormat = new SimpleDateFormat("MMM DDD yyyy");
try {
Date originaldate = originalFormat.parse(date.toString());
finalDate = originaldate.toString();
} catch (java.text.ParseException e1) {
e1.printStackTrace();
}
but keep getting a ParseException for "Unparseable date", any idea what's going on? If I were to simply change this line back to this:
SimpleDateFormat originalFormat = new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyyy");
Then it prints out the full date again just fine with no parse exception, including all the date stuff I don't want.
Don't use parse method, use format instead :
Date date = object.getCreatedAt();
SimpleDateFormat formater = new SimpleDateFormat("M/d/yyyy");
String datestring = formater.format(date); // value is : 3/17/2014
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString()
java.util.Date.toString() method always returns a String of format
dow mon dd hh:mm:ss zzz yyyy
For example, Thu Jan 10 02:00:00 EET 1992.
Your Date format "MMM DDD yyyy" expects a date String like Jan 10 1992 where 10 represents not 10th day of January but 10th day of year 1992.
Therefore to convert a date to String and convert it back to Date object using your format, you need to do
Date originaldate = originalFormat.parse(originalFormat.parse(date.toString()));
Or to convert Date.toString() to Date object,
SimpleDateFormat toStringFormat = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date originaldate = toStringFormat.parse(date.toString());
Lastly, if you want a Date string with format like 3/17/2014, the correct format is M/d/yyyy.
Refer to http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for help on how to write Date format.
I'm sure this is a simple one!
I've got this String String date = "Wed, 2 Jan 2013 12:17:15 +0000 (GMT)" which I want to parse to a Date to be able to set an JavaMail's sent date.
Here's my full code
String dateString = "Wed, 2 Jan 2013 12:17:15 +0000 (GMT)";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date date = sdf.parse(dateString);
System.out.println("Date: " + date.toString());
email.setSentDate(oDate); // Assume email is initialised correctly
Expected output
Date: Wed, 2 Jan 2013 12:17:15 +0000 (GMT)
Actual output
Date: Wed Jan 02 12:17:15 GMT 2013
I'm not even bothered about the time component, as long as my email appears to be from the correct date.
Try this:
String reformattedStr = sdf.format(sdf.parse(dateString));
or this
String reformattedStr = sdf.format(sdf.parse(date.toString()));
Date.toString() applies its own format when converting to string:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
You should use DateFormat to get a desired string, ie for a posted code example:
System.out.println(sdf.format(date));
The setSentDate method will format the date properly for an email message before setting it in the email message. That's different than what Date.toString() does. See also the MailDateFormat class.
I have used the following
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss zzz");
Date date = new Date();
String formattedDate= df.format(date);
Date dateWithTime = df.parse(formattedDate);
i got the formatted date as string when i conver this into date i got error like
java.text.ParseException: Unparseable date: "Tue Feb 26 11:45:43 IST 2013"
How would convert to date or how i format a current date and get as date?
I think your code wouldn't throw ParseException. But it sure would definitely yield wrong output. your format should be:
"dd-MM-yyyy hh:mm:ss zzz"
Note that
MM---> Months
mm---> Minutes
Test with your code with out correcting the format:
Sat Jan 26 06:24:07 GMT 2013
Test with your code with correcting the format:
Tue Feb 26 06:20:51 GMT 2013
The date format should be as follows as shown in the exception. Change it to -
EEE MMM d hh:mm:ss z yyyy
The correct simpledateformat will be
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Please refer the link for proper date formatting and parsing
SimpleDateFormat
I have the following string:
Mon Sep 14 15:24:40 UTC 2009
I need to format it into a string like this:
14/9/2009
How do I do it in Java?
Use SimpleDateFormat (click the javadoc link to see patterns) to parse the string in one pattern to a fullworthy Date and use another one to format the parsed Date to a string in another pattern.
String string1 = "Mon Sep 14 15:24:40 UTC 2009";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy").parse(string1);
String string2 = new SimpleDateFormat("d/M/yyyy").format(date);
System.out.println(string2); // 14/9/2009
You can use SimpleDateFormat class to convert the string you have to a date object. The date format can be given in the constructor. The format method converts the string to a date object.
After getting the date object, you can format it in the way you want.
One liner in java 8 and above.
String localDateTime= LocalDateTime.parse("Mon Sep 14 15:24:40 UTC 2009", DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss z yyyy")).format(DateTimeFormatter.ofPattern("d/M/yyyy"));
Date d = new Date("Mon Sep 14 15:24:40 UTC 2009");
SimpleDateFormat f = new SimpleDateFormat("dd/M/yyyy");
String s = new String(f.format(d));