This question already has answers here:
Java String to Date, ParseException
(4 answers)
Closed 7 years ago.
I have a date string as
"Wed Jul 01 08:16:13 PDT 2015"
I am trying to parse it with this SimpleDateFormat
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
this way:
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
It is giving me parse error as:
java.text.ParseException: Unparseable date: "Wed Jul 01 08:16:13 PDT 2015" (at offset 0)
How can I get a date in above simple date format from the string
Try this:
DateFormat originalFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = originalFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
I suspect you don't realy understand the consept of the SimpleDateFormat.
After you define the template with :
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
The valueDateFormat could parse Date object acording to it, it not take just a String you have and convert it. it take Date object.
Date
Your date string ("Wed Jul 01 08:16:13 PDT 2015") doesn't match your pattern ("yyyy-MM-dd hh:mm:ss")
Write correct pattern which matches date string (first goes Day of week, than month in year, etc.)
try this..
public static void main(String[] a)
{
SimpleDateFormat valueDateFormat = new SimpleDateFormat("EEE MMM yy hh:mm:ss");
try {
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
System.out.println(parsedDate1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For the formats like this I created a helper method:
public Date parseString(String date) {
String value = date.replaceFirst("\\D+([^\\)]+).+", "$1");
//Timezone could be either positive or negative
String[] timeComponents = value.split("[\\-\\+]");
long time = Long.parseLong(timeComponents[0]);
int timeZoneOffset = Integer.valueOf(timeComponents[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
//If Timezone is negative
if(value.indexOf("-") > 0){
timeZoneOffset *= -1;
}
//Remember that time could be either positive or negative (ie: date before 1/1/1970)
//time += timeZoneOffset;
return new Date(time);
}
It returns date object from Date string so you can format your string like this:
Date date = parseString("Wed Jul 01 08:16:13 PDT 2015");
And after that you can easily format given date variable.
Related
I have a time raw string, "2016-05-15T12:42:00.000-04:00" and I want to convert the string to "Wed 15 May 2016 12:42", which keeps the same timezone (-04:00) as its original source.
I have tried SimpleDateFormat but using it returns different timezones that are not the same as the timezone in my original string. Please help me achieve this in Android Studio!
Other examples:
2016-05-15T15:42:00.000-08:00 -> Wed 15 May 2016 15:42
2016-05-15T14:44:00.000-01:00 -> Wed 15 May 2016 14:44
public static String formatDateString(String originDateString) {
//Original format 2016-05-15T12:42:00.000-04:00
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat resultFormat = new SimpleDateFormat("EEE dd MMM yyyy HH:mm");
String dateString = "";
try {
Date date = originalFormat.parse(originDateString);
dateString = resultFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//returned format: Tue 14 May 2016 12:42
return dateString;
}
try saving it first without the TZ information
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
I am receiving a Java Date formatted like so: "Sun Sep 14 02:00:00 PDT 2014" into a yyyy-MM-dd format but I can't seem to parse it. What I tried is the following:
String time = "Sun Sep 14 02:00:00 PDT 2014";
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date contractEffectiveDateFormat = f.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
However, I get an error saying that this date is unparsable. I'm not sure how to go about parsing this date because if I try to parse the date using the following:
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
which is how to actually parse the date correctly into a Date object,
the string would turn into a Date object, but I can't seem to do anything with it from there. I want to turn it in so that it looks like 2014-09-14. Any ideas on how to do so? Thanks!
Use two DateFormat(s) one for input and for output,
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
DateFormat in = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
Date effectiveDate = in.parse(time);
System.out.println("Date: " + out.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is the requested
Date: 2014-09-14
Your incoming string is this String time = "Sun Sep 14 02:00:00 PDT 2014";
which means the SimpleDateFormat pattern should match the incoming String pattern so you need to use SimpleDateFormat like this
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
Then when you called parse() on inFormat it will give you Date Object which doesnot have particular format associated with it. So in order to format the Date again you need to create SimpleDateFormat object specifying the format you want which is this
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Ultimately bind all together
One more thing always specify the Locale
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// good practice to specify the locale
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
try {
Date parsedDate = inFormat.parse(time);
System.out.println("Required Formatted Date: " + outFormat.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Simply add another SimpleDateFormat that'll allow you to present the Date object the way you want:
public static void main(String[] args) throws ParseException {
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date contractEffectiveDateFormat = df.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(f.format(contractEffectiveDateFormat)); // prints 2014-09-14
}
I would like to extract date and year from the following string and convert it to a Data Object in Java.
Mon Jul 07 19:18:26 CEST 2014
How can I extract only date and year (in this case, 2014-07-07) from the text in a sophisticated way?
SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yyyy");
String dateInString = "Mon Jul 07 19:18:26 CEST 2014";
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = s.parse(dateInString.split(" ")[2]+"/"+dateInString.split(" ")[1]+"/"+dateInString.split(" ")[5]);
System.out.println(new SimpleDateFormat("YYYY-MM-dd").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should work for you, I splitted your String, then put it to a date format and then formatted it the way you wanted it to be, assumed you wanted the months as the second parameter after the year, if thats not the case you can simply change the 'MM' to 'dd' and the 'dd' to 'MM'.
For Java 7 or below, use a SimpleDateFormat for parsing and formatting:
Locale dateLocale = Locale.US;
SimpleDateFormat inFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", dateLocale);
Date date = inFormat.parse("Mon Jul 07 19:18:26 CEST 2014");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", dateLocale);
System.out.println(outFormat.format(date));
Since Java 8, you can use DateTimeFormatter:
Locale dateLocale = Locale.US;
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", dateLocale);
TemporalAccessor date = inFormatter.parse("Mon Jul 07 19:18:26 CEST 2014");
DateTimeFormatter outFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
System.out.println(outFormatter.format(date));
All,
I need to convert the following string "Tue Jan 01 08:00:00 CET 2013" to a date object with format "dd/MM/yyyy HH:mm".
What I have done till now...
String dateStr = "Tue Jan 01 08:00:00 CET 2013";
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat writeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
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);
But this gives me a String as a result and not a date. If I parse the formattedDate String again using the writeFormat then I get the same original date back again i.e. Tue Jan 01 08:00:00 CET 2013.
NOTE: Finally, I want to push the date into MySQL DateTime datatype via Java Date object. i.e. String -> Java Date -> MySQL Date Time.
I have searched High/Low on the web and could not find a proper solution. Please help!!!
Thanks and regards,
SG
I found the answer.
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
date = readFormat.parse(string);
And then while saving to MYSQL database...
pstmt.setDate(5, new java.sql.Date(change.getEndDateTime().getTime()));
Thanks for all your responses and apologies for posting duplicate if it is...
Regards,
SG
I have a date string:
Thu Feb 20 08:00:00 EET 1992
And using this code to format it:
String datePatternFrom = "EEE MMM dd HH:mm:ss ZZZ yyyy";
String datePatternTo = "MMM dd, yyyy";
String prettyDate = "";
try {
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom);
Date date = (Date)fromFormatter.parse(userBirthday.toString());
DateFormat toFormatter = new SimpleDateFormat(datePatternTo);
prettyDate = toFormatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Why I'am getting the exception?
java.text.ParseException: Unparseable date: "Thu Feb 20 08:00:00 EET 1992" (at offset 0)
The problem is with the weekday and month and your locale.
Thu is English, so you have to tell the parser that it should use English weekdays:
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom, Locale.US);
This will work for your pattern.
If you do not specify a locale, the default will be used, which is not always an English one. ;-)
It could be your locale. Try making a SDF with datePatternFrom, give it a date to format and print that somewhere. See what pops up.
Probably your userBirthday object was not created as a java.util.Date object. Can you try a System.out.println(userBirthday.getClass().getName());?