Java String to Date unparsable date - java

This is the String I'm extracting the info from using a regex:
2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message
for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
This is the code I use to extract the String and then try to make it into a Date:
Pattern pattern3 = Pattern.compile(";\\s(.*)");
Matcher matcher3 = pattern3.matcher(s);
matcher3.find();
String t = matcher3.group(1).toString();
try {
Date time = new SimpleDateFormat("dd/MMM/yy hh:mm a").parse(t);
} catch (ParseException e) {
e.printStackTrace();
}
This should be the format of my input:
Thu Oct 25 12:08:25 2012 +0000
And what I want is to make a Date from the aforementioned string which looks like:
25/Oct/12 12:08 PM
But I keep getting these errors:
java.text.ParseException: Unparseable date: "Thu Oct 25 12:08:25 2012 +0000"
Fixed the message for defaulted bonds0null
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)
java.text.ParseException: Unparseable date: "Thu Oct 25 11:52:39 2012 +0000"
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)

Your pattern has to match the pattern of the incoming data, which it doesn't right now.
SimpleDataFormat can't read your mind, the pattern you are giving it doesn't match the format you are passing into .parse().
"dd/MMM/yy hh:mm a" will never match Thu Oct 25 12:08:25 2012 +0000, you have to specify the exact pattern that the incoming data is in, this is very well documented in the JavaDocs.
Then you can change the pattern to what you want using .applyPattern() can call .format() to get the formatted output you want.

I would simply remove the unwanted part:
String dateAsString = s.replaceAll(".*;\\s+","");
Then you need to DateFormat: one to parse the string and another one to output the correct format:
String s = "2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000";
System.out.println("s = " + s);
String dateAsString = s.replaceAll(".*;\\s+","");
System.out.println("dateAsString = " + dateAsString);
DateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy X", Locale.ENGLISH);
Date date = parser.parse(dateAsString);
System.out.println("date = " + date);
DateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy hh:mm a", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formatter.format(date));
outputs:
s = 2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
dateAsString = Thu Oct 25 12:08:25 2012 +0000
date = Thu Oct 25 14:08:25 CEST 2012
25/Oct/2012 12:08 PM
Note: you need to use the appropriate locale to parse and print the month/day names

Related

How to select only date with JXDatepicker

When I select date in SQL it is returned as:
Wed Jan 31 00:00:00 GMT+01:00 2018
But I need only the Date part, that is Jan 31 2018. How can I do this?
There may be a reason to use org.jdesktop.swingx.JXDatePicker but rather of using JXDatePicker, I will simply show GMT parsing using java.util.Date.
Try this source code:
System.out.println(new Date());
//will show your Date along with your local TimeZone
//result for me is : Sat Jan 13 08:47:59 IST 2018
//First changing local pacific time zone to GMT+01 level explicitly,
//otherwise it will show results as your local time zone by default.
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
String existingDateValue = "Wed Jan 31 00:00:00 GMT+01:00 2018";
DateFormat gmtFormat =
new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
try {
//try to parse and validate existing date
Date validatedExistingDate = gmtFormat.parse(existingDateValue);
System.out.println(validatedExistingDate);
//parsed validated date : Wed Jan 31 00:00:00 GMT+01:00 2018
DateFormat newFormat = new SimpleDateFormat("MMM d, yyyy");
System.out.println(newFormat.format(validatedExistingDate));
//required Date is in GMT format : Jan 31, 2018
} catch (ParseException pex) {
pex.printStackTrace();
} finally {
System.out.println("Current TimeZone : " + new Date());
//now, reverting to my local TimeZone
TimeZone.setDefault(TimeZone.getTimeZone("IST"));
System.out.println("Current TimeZone : " + new Date());
}

Java Unparseable date: "Fri Nov 10 22:50:46 EET 2017" exception

I am trying to get "dd.MM.yy H:m" pattern of a date.
And I use the code given below
String dDate = "Fri Nov 10 22:50:46 EET 2017";
DateFormat dfr = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Date cDate = dfr.parse(dDate);
DateFormat df = new SimpleDateFormat("dd.MM.yy H:m");
String result = df.format(cDate);
lastSyncDate.setText(getResources().getString(R.string.last_backup) + " " + result);
And I get
java.text.ParseException: Unparseable date: "Fri Nov 10 22:50:46 EET 2017" exception.
I cannot solve it and I think it is about DateFormat pattern.
Thank you for your solvings.

Parsing the date, and converting to DATE for MYSQL? [duplicate]

This question already has answers here:
How to get a Date object from String
(5 answers)
Closed 8 years ago.
So I am using DateFormat to convert a date (that I parsed form a CSV) to YYYY-MM-DD format, so I can INSERT it into a MYSQL column which is in DATE format.
I am using DateFormat, but my date output keeps coming out like this:
Mon Dec 29 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Wed Dec 31 00:00:00 CST 2014
How can I convert this date to the format I am looking for?
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.parse(date_parsed[0].toString());
System.out.println("DATE=" + date);
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "Tue Dec 30 00:00:00 CST 2014";
Date date = formatter.parse( dateStr );
formatter.applyPattern("yyyy-MM-dd");
String newDate = formatter.format( date );
System.out.println("DATE=" + newDate);
Output :
DATE=2014-12-30

Convert Format in array of type Date to different Date format?

I have the following:
String[] string_dates = new String[10]
// read in strings formatted via fmt1 dd-MMM-yy)
Date[] date_dates = new Date[10];
for (int i = 0; i < 9; i++){
date_dates[i] = fmt1.parse(string_dates[i]);
What would be the most efficient way to format the Dates in date_dates[] to format dd-MM-yyyy? Should I convert the Strings in strings_dates[] to format dd-MM-yyyy first, and then read them into dates? Thank you.
A Date is the representation of the number of milliseconds since the Unix epoch. It has no concept of a format of it's own (other then that created by toString, which should not worry about)...
Once you have converted the String representation of the date to a Date, you should then use an appropriate formatter to format that date in what ever format you want...
String[] stringDates = {
"01-MAR-2013",
"02-MAR-2013",
"03-MAR-2013",
"04-MAR-2013",
"05-MAR-2013",
"06-MAR-2013",
"07-MAR-2013",
"08-MAR-2013",
"09-MAR-2013",
"10-MAR-2013"};
SimpleDateFormat inFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date[] dates = new Date[stringDates.length];
for (int i = 0; i < stringDates.length; i++) {
try {
dates[i] = inFormat.parse(stringDates[i]);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
SimpleDateFormat outFormat = new SimpleDateFormat("dd-MM-yyyy");
for (Date date : dates) {
System.out.println("[" + date + "] - [" + outFormat.format(date) + "]");
}
Which produces...
[Fri Mar 01 00:00:00 EST 2013] - [01-03-2013]
[Sat Mar 02 00:00:00 EST 2013] - [02-03-2013]
[Sun Mar 03 00:00:00 EST 2013] - [03-03-2013]
[Mon Mar 04 00:00:00 EST 2013] - [04-03-2013]
[Tue Mar 05 00:00:00 EST 2013] - [05-03-2013]
[Wed Mar 06 00:00:00 EST 2013] - [06-03-2013]
[Thu Mar 07 00:00:00 EST 2013] - [07-03-2013]
[Fri Mar 08 00:00:00 EST 2013] - [08-03-2013]
[Sat Mar 09 00:00:00 EST 2013] - [09-03-2013]
[Sun Mar 10 00:00:00 EST 2013] - [10-03-2013]
You should avoid the temptation to save the formatted Date and instead simply keep the Date object and format it as you need.
you can format the string in to date type using following SimpledateFormat in java. Following is the example
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-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();
}
Note
For complete date and time patterns, please refer to this java.text.SimpleDateFormat JavaDoc.
Well in you case please take a look at these resourcesclick here
You can give "dd-MM-yyy". Please try it

Java Parse Date w/ SimpleDateFormat

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.

Categories

Resources