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);
I get Unparseable error when I run below code. How can I convert dd MMM yyyy format to dd/MM/yyyy format?
public Calendar myMethod(){
String dateStr = "16 Dec 2014"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date thedate = formatter.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(thedate);
return cal;
}
Assuming you fix the blatant syntax errors, then:
String dateStr = "16 Dec 2014" // <== This date is in dd MMM yyyy
formatter = new SimpleDateFormat(dd/MM/yyyy); // <== This SDF is in dd/MM/yyyy
Date thedate = formatter.parse(dateStr); // <== So how can it be expected to parse?
What you do is create a parser for the format you need to parse, and a formatter for the format you want:
String dateStr = "16 Dec 2014";
SimpleDateFormat parser = new SimpleDateFormat("dd MMM yyyy");
Date thedate = parser.parse(dateStr);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateStrReformatted = formatter.format(thedate);
I can't figure out why this is returning
Wed Jul 02 18:21:27 CDT 2014
instead of
07/02/14 6:21 pm
pubdate = Mon, 30 Jun 2014 22:37:15 +0000
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
newFormat.format(dateFormat.parse(pubDate));
this.pubDate = date;
}
If you want to print the format you want, you have to use String to represent your date, otherwise, Date type will always print this format "dow mon dd hh:mm:ss zzz yyyy"
this.pubDate = date;//Assign the reference of date Object
//this.pubDate will have value of date NOT Format :)
But here format won't be passed to pubDate as that will remain as it is.
If you want to make your pubDate to have dd/Mm/yyyy aa format you have to format the pubDate as well here you are only assigning reference from one date to other but formation on one date won't affect the other one you have to apply that to this.pubDate whenever you want to use pubDate.
You can declare general format(Class level Object) and use it in your program whenever you want to display the date.
Because Date has toString() which per the Javadoc,
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
where:
dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
dd is the day of the month (01 through 31), as two decimal digits.
hh is the hour of the day (00 through 23), as two decimal digits.
mm is the minute within the hour (00 through 59), as two decimal digits.
ss is the second within the minute (00 through 61, as two decimal digits.
zzz is the time zone (and may reflect daylight saving time). Standard time zone
abbreviations include those recognized by the method parse. If time zone
information is not available, then zzz is empty - that is, it consists of no
characters at all.
yyyy is the year, as four decimal digits.
When you want to deviate from that, you will need your newFormat -
// As a String
System.out.println(newFormat.format(dateFormat.parse(pubDate)));
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
return newFormat.format(dateFormat.parse(pubDate));
}
Use corrected code below:
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
System.out.println("Formatted date is ="+ newFormat.format(x));
}
Try this, Create your custom date class
public class MyDate extends Date
{
#Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy hh:mm aa");
return dateFormat.format(new Date());
}
}
then print the object like
System.out.println(new MyDate());
I have date strings in this form Thu Aug 02 00:00:00 GMT+00:00 2012
I have tried to use this method to parse these String in a Date object
public Date fromStringToDate(String data) {
Date result;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
try {
result = sdf.parse(data);
return result;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
But doesn't works and I get this error
java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"
I suppose that the problem is caused by a wrong SimpleDateFormat, but I don't know the right syntax to fix it.
You need to adjust the date format to the given string:
EEE MMM dd HH:mm:ss Z yyyy
Make sure use the correct placeholders, case sensitive, etc. Take a look to the Date and Time Patterns.
Sorry, I had a mistake with the 'z' pattern, 'Z' is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Take a look to Locale.US, it is important to apply because the months and and days are in english.
Use this date formatting:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
I'm parsing a UTC Date string with SimpleDateFormat and it is parsed as the previous date.
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
This string "Fri, 06 Apr 2012 04:00:00 GMT" is being parsed as 4/5/2012. Why? Thanks.
Below is example of date conversion...
For your program, do changes accordingly and let me know what output you are getting.
String dateStr = "Thu Jan 19 2012 01:00 PM";
DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
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);
Output is 2012-01-19 13:00:00
Do the changes as per output you are expecting.
Beware of JDK 1.5.0_22 (linux). Produces incorrect date. Switched to JDK 1.6 problem gone.