Is this a Valid DateFormat in Java - java

I want to parse this date "Mon Mar 09 2015 00:00:00 GMT+0530 (India Standard Time)" in java SimpleDateFormat class as "MM-dd-yyyy", is this a valid date format? If yes, how can I do that?
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
//SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss"); -- Not working
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z"); // Not Working
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));

Tried following piece of code:
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z");
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Output:
2014-09-17

Try by using this format "E MMM dd yyyy HH:mm:ss z" with SimpleDateFormat class.

Related

Prevent invalid date from getting converted into date of next month in jdk6?

Consider the snippet:
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
gives me the output as
01.02.2015 // Ist February 2015
I wish to prevent this to make the user aware on the UI that is an invalid date?
Any suggestions?
The option setLenient() of your SimpleDateFormat is what you are looking for.
After you set isLenient to false, it will only accept correctly formatted dates anymore, and throw a ParseException in other cases.
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
try {
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
} catch (ParseException e) {
// Your date is invalid
}
You can use DateFormat.setLenient(boolean) to (from the Javadoc) with strict parsing, inputs must match this object's format.
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
ddMMyyyy.setLenient(false);
Set the date formatter not to be lenient...
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);

How do I parse this date format?

How do I parse this date format:
"Tue Dec 16 07:01:31 CET 2014"
I tried the following:
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.CANADA);
But that doesn't seem to be correct. Do you recognize this format and know how I need to configure DateFormat?
Try this;
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Date d = parserSDF.parse("Tue Dec 16 07:01:31 CET 2014");

java.text.Parseexception, unparseble date at position 0? Android

I am using the below code to parse a date, and to format it how I want; the code is working fine for other dates such as this one:
Thu, 04 Sep 2014 19:32:00 GMT
But it is throwing the above error message on a date such as this:
Fri, 05 Sep 2014 06:01:00 +0100
Can anyone explain why?
Thanks.
The code:
//Only get the 'important' parts.
pubDateFormat = pubDate.substring(0, 23);
//Format it how I want.
dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm");
//Here is where it is breaking.
Date date = dateFormatter.parse(pubDateFormat);
//Format it once again to how I want.
dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
//get the current date and format it the same.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
currDate = sdf.format(new Date());
currDate = currDate.replace(" ", "");
rssDate = dateFormatter.format(date).replace(" ", "");
This is the log:
09-05 11:30:07.153: E/DATE(7665): Fri, 05 Sep 2014 06:01
09-05 11:30:07.153: E/ERROR(7665): java.text.ParseException: Unparseable date: "
09-05 11:30:07.153: E/ERROR(7665): Fri, 05 Sep 2014 06:01" (at offset 0)
You have to add the locale:
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.ENGLISH);
Try this
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.US);

How to parse the string "Mon Oct 22 03:00:26 +0000 2012" [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
How to parse the following string to date
Mon Oct 22 03:00:26 +0000 2012
I tried MMM dd HH:mm:ss yyyy, but it is not working. I know I am missing something but couldn't find out that.
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
Date d=(Date)a.parse(b)
I'd recommend EEE MMM dd HH:mm:ss Z yyyy instead of HH:mm:ss yyyy.
Edit:
Specifically, your code would be:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy");
Date d=(Date)a.parse(b)
Edit after comment:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy", Locale.getDefault());
Date d=(Date)a.parse(b)
Try to do something like this:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Ref: How to Convert String to Date in Java

Java date format convert on Android

I have date String as below:
Fri, 19 Jul 2013 01:30:22 GMT
Thu, 12 Sep 2013 19:19:18 GMT
And I want to convert to below format:
2013/07/19 01:30:22
2013/09/12 19:19:18
At first, I use below code to parse date:
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
But it show below exception on line Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");:
java.text.ParseException: Unparseable date: "Fri, 23 Aug 2013 01:10:35 GMT" (at offset 0)
How can I modify it?
try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
your default language is probably not English and does not accept Fri and Aug
Use this code
String dateString = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date;
try {
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateString);
Log.e("result", "Date:" + simple.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this
String DateStr="Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(DateStr);
System.out.println(sim.format(d));
You can make like this.
String newDate = date.getYear()+"/"+date.getMonth()+"/"+date.getDay() and ect.
or you can write your own convert method
String myConvert(Date date)
{
return date.getYear()+"/"+date.getMonth()+"/"+date.getDay()+ect.
}
You can try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
String formattedString=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").
format(date).toString();
System.out.println(formattedString);
Try
String dateStr = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(dateStr);
System.out.println(sdf.format(date));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date.parse("Fri, 19 Jul 2013 01:30:22 GMT");
String newDate = simpleDateFormat.format(date);
Try with "format" :
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "";
try {
date = dateFormat.format("Fri, 23 Aug 2013 01:10:35 GMT");
} catch(Exception e){
e.printStackTrace();
}

Categories

Resources