Date formatting is not formatting correctly - java

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());

Related

How to resolve Unparseable date error

I am receiving date from the RSS Feed in the below format
Fri Oct 23 11:07:08 IST 2015 which i am trying to convert it into
yyyy-MM-dd HH:mm format .
I have tried this way
public class ConvertDate {
public static void main(String args[]) throws ParseException
{
String passedate = "Fri Oct 23 11:07:08 IST 2015";
String res= convertdate(passedate);
System.out.println(res);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate;
}
}
But i am getting
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Oct 23 11:07:08 IST 2015"
at java.text.DateFormat.parse(Unknown Source)
at ConvertDate.convertdate(ConvertDate.java:20)
at ConvertDate.main(ConvertDate.java:12)
Could you please let em know how to resolve this
The date pattern does not match the input. Try change the line
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
to
SimpleDateFormat in = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Hope that helps
Your date has the format EEE MMM dd HH:mm:ss zzz yyyy with an english local.
This parses the date correctly:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)
.parse("Fri Oct 23 11:07:08 IST 2015");
Try this:
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
in.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem
String s2 = "Fri Oct 23 11:07:08 IST 2015";
Date date = in.parse(s2);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
out.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(out.format(date));
Output:
2015-10-23 11:07
Also, note the setTimeZone. IST can either stand for Indian ST or Israel ST so it would be better if you specify which time zone you really want.
Check here for IST ambiguity.
First check your actual date which you need to work .. In my case
String day="date:10/01/2018";(In selenium need to get it from web page so i got the above string from page)
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);
When am working on this i got unparsable error....
So the day string contains some part of characters . So just remove those characters from string by using day.split(":"); String day1=day[1];
just give this day1 string in parse(); Now the updated code like as below..
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day1);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);

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);

Can't parse string to date

Server return me date in format "Sat, 10 Jan 2015 07:24:00 +0100".
I try to parse this string to date, but it was unsuccessful.
This my code of parsing:
SimpleDateFormat format = new SimpleDateFormat("dd.Mm.yyyy");
try {
Date date = format.parse("Sat, 10 Jan 2015 07:24:00 +0100");
tvDate.setText(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
This is the format that you want to use:
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Why?
The documentation goes over the symbols, but for the most part...
EEE matches a shorthand day
dd matches a two-digit date (so 01 through 31)
MMM matches a three-letter month (so Jan)
yyyy matches a four-letter year
HH:mm:ss Z is shorthand (enough) for the full 24-hour clock with Z representing the offset from GMT.
You should use a format like this if you don't care about the +0100:
SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss");
E - is day of week like "Sat"
d - day of month
M - is month
y - is year
h - is hour
m - is minute
s - is second
If you really care about the timezone, what you need to do is changing the String format of your SimpleDateFormat instance into something that represents the date String that is being returned.
Here is an example:
public static Date stringToDate(String dateString) throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
return format.parse(dateString);
}
public static void main(final String[] args) throws ParseException {
Date example = stringToDate(
"Sat, 10 Jan 2015 07:24:00 +0100");
}
You might also want to consider that SimpleDateFormat is not thread-safe and could cause unexpected behavior if not used properly. Here is a very useful explanation about this:
http://javarevisited.blogspot.com/2012/03/simpledateformat-in-java-is-not-thread.html

Formatting Date from Date Object with only MM/DD/YYYY

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.

Parsing String with spaces to Date, lead to ParseException

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")

Categories

Resources