SimpleDateFormat function parse(String s) gives wrong date - java

As an input I have Date object(for example, exDate=Fri Aug 01 00:00:00 EEST 2014) that must be formated. After the parsing of the date, I get wrong date.
SimpleDateFormat sdf = new SimpleDateFormat(
"dd-MMM-YYYY hh.mm.ss.SSSSSSSSS aa", Locale.ENGLISH);
String dateStart = sdf.format(exDate);
Date dateF = sdf.parse(dateStart);
dateStart will be equal to
01-Aug-2014 12.00.00.000000000 AM
and the resut, dateF will be equal to
Sun Dec 29 00:00:00 EET 2013
So, after the parsing of a string with date, the result is wrong.
Maybe, somebody know the source of the problem? Or another way to format date in another SimpleDateFormat?

The problem is the YYYY which means:
Y Week year;
The actual year, which is what you are looking for would be yyyy.
I really recommend that you go in the link above to see the full list.
You should also replace the milliseconds to .SSS as you can't get more precise than that.

Related

How to change date format from yyyy-MM-dd to dd-MMM-yyyy

I am retrieving date from database. Datatype is'date' with date format 2015-12-16.
I need to set that date to my bean class variable.Datatype is Date with format 16-Dec-2015
These are the date formats i am using
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String formatteddate = null;
try {
formatteddate = formatter.format(rs.getDate("dol"));
System.out.println("formatteddate=============="+formatteddate);
date = formatter.parse(formatteddate);
System.out.println("date========="+date);
} catch (ParseException e) {
e.printStackTrace();
}
joborderbean.setDol(date);
formatteddate==============15-Dec-2015
I want to display the above format i.e,15-Dec-2015. But it is diplaying the below format
date=========Tue Dec 15 00:00:00 IST 2015
Please help me
Do like following:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateformat=fromatter.format(mysqldate);
System.out.println("first date format"+ dateformat);
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
String dateformat1=formatter1.format(mysqldate);
System.out.println("second date format"+dateforamt1);
also you can edit the sql fired to get the result like
select DATE_FORMAT(date_field,'%Y-%b-%d')
and
select DATE_FORMAT(date_field,'%d-%b-%Y')
First of all the Resultset objects getDate method return a java.sql.Date object. So in order to store the Date in the date object use :
date = rs.getDate("dol");
instead of the code:
formatteddate = formatter.format(rs.getDate("dol"));
System.out.println("formatteddate=============="+formatteddate);
date = formatter.parse(formatteddate);
System.out.println("date========="+date);
now in order to test the date you can use:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
System.out.printLn("date========="+formatter.format(date));
This will give you the required output.
The last output you are getting because of the code System.out.println("date========="+date); Here the toString method of the date object is getting called which has a default as in the documentation as :
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.

how can i get current system date in Android?

I am getting date in 28/12/2013 format, but I need current date in a String format,
like
Thursday, August 21
so that I can set over TextView,
Explain a bit, if you think something is necessary, I am new to Android Development.
You can always refer to the documentation.
http://developer.android.com/reference/java/util/Date.html
In the documentation you will find this:
Calendar.get(Calendar.MONTH)
public int getMonth () #old do not use
Returns the gregorian calendar month for this Date object.
Calendar.get(Calendar.YEAR
public int getYear () #old do not use
Returns the gregorian calendar year since 1900 for this Date object.
Calendar.get(Calendar.DAY_OF_WEEK)
public int getDay () #old do not use
Returns the gregorian calendar day of the week for this Date object.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMM dd");
String formatedDate = sdf.format(your_date);
At the moment I haven't programmed any android app, I'll do that in the future. But I have found that here. It may soulve your problem hopefully.
DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(),
DateFormat.getDateTimeInstance(),
DateFormat.getTimeInstance(),
};
for (DateFormat df : formats) {
System.out.println(df.format(new Date(0)));
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(df.format(new Date(0)));
}
Produces this output when run on an en_US device in the America/Los_Angeles time zone:
Dec 31, 1969
Jan 1, 1970
Dec 31, 1969 4:00:00 PM
Jan 1, 1970 12:00:00 AM
4:00:00 PM
12:00:00 AM

how to compare two dates in java in a simple way

I want to know if there is a simple way to compare two dates of this format for example :
Wed, 31 Jul 2013 09:31:51
Mon, 05 Aug 2013 10:18:24
and display the greatest date?
I'd suggest you to use Joda library. Using the date info you have, create DateTime instances and call isBefore() method to determine which one comes first.
I would check the documentation, which shows that Date implements Comparable<Date> and so
date1.compareTo(date2);
will do what you want. You may wish to ensure that date1 is not null.
If your dates are (in fact) Strings, then use SimpleDateFormat's parse() method to convert from strings to dates, and then perform that comparison.
As others have suggested, Joda is a better date/time library (better API and threading performance).
Date d1, d2;
This returns greatest dates:
d1.after(d2) ? d1 : d2;
first parse the string into a Date object using a SimpleDateFormat :
String dateStringA = "Wed, 31 Jul 2013 09:31:51";
String dateStringB = "Mon, 05 Aug 2013 10:18:24";
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE, DD MMM yyyy HH:mm:ss");
Date dateA = parserSDF.parse(dateStringA);
Date dateB = parserSDF.parse(dateStringB);
if (dateA.compareTo(dateB) > 0) {
System.out.println("A bigger");
}
then compare the Date objects using compareTo method
You can use Date's getTime() method and then use < >.
This will work for you
DateFormat df=new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
Date date1=df.parse("Wed, 31 Jul 2013 09:31:51");
Date date2=df.parse("Mon, 05 Aug 2013 10:18:24");
System.out.println(date1.after(date2) ? date1 : date2);

SimpleDateFormat parse function changing the format

I have a String with several dates:
[20-Jul-2012 5:11:36,670 UTC PM, 20-Jul-2012 5:11:36,683 UTC PM]
ParsePosition parsePos = new ParsePosition(1);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");
System.out.println(format2.parse(entry.getValue().toString(), parsePos)) ;
Output : Fri Jul 20 06:11:36 BST 2012
I need the output to be 20-Jul-2012 5:11:36,670 UTC PM.
Do I need to set a LOCALE in the SimpleDateFormat to not have a different output?
You need to set the time zone, but more importantly, you simply need to actually use the format to format the date:
Date date = format2.parse(...);
String formattedDate = format2.format(date);
System.out.println(formattedDate);
What your code does is:
Date date = format2.parse(...);
System.out.println(date.toString());
I don't really understand the point in parsing a string to a date, and then displaying the date using the exact same format, though (except to validate that the String is indeed a valid date, but then you could simply reuse the original string).
You've got two small problems:
Use hh for the hour, not HH. H is "Hour in day (0-23), and so will not work correctly with a, the AM/PM marker. Your two example date strings will parse to AM, not PM.
You're using SimpleDateFormat to parse the string, but not format it. Use format2.format(format2.parse(entry.getValue().toString()).
Here's a complete example:
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss,SSS z a");
String input = "20-Jul-2012 5:11:36,670 UTC PM";
Date date = format.parse(input);
String output = format.format(date);
System.out.println(output);
Result:
20-Jul-2012 05:11:36,670 UTC PM

Java - unparseable date, need format to match "GMT-0400"

I have the following Java:
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd yyyy HH:mm:ss zZ (zzzz)", Locale.ENGLISH);
Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.APRIL, 1);
out.println(formatter.format(cal.getTime()));
out.println();
Date date;
try {
date = formatter
.parse("Fri Apr 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)");
} catch (ParseException e) {
out.println("Failed to parse date: " + e.getMessage());
e.printStackTrace(out);
}
This is in a servlet, and the Calendar-constructed date comes out as:
Fri Apr 01 2011 16:42:24 EDT-0400 (Eastern Daylight Time)
This looks like the same format as the date string I'm trying to parse, except for EDT-0400 versus the desired GMT-0400. The code fails when trying to parse the date string:
java.text.ParseException: Unparseable date: "Fri Apr 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)"
How can I parse such a string? This is coming from a JavaScript date in a Sencha Touch 1.1.1 model, stored in WebSQL local storage.
For some reason GMT-0400 isnt' working, and UTC-0400 is working. You can replace GMT with UTC.
Note that this part will be completely ignored - the timezone will be resolved from what's found in the brackets (at least on my machine, JDK 6)
I debugged SimpleDateFormat and it seems that it will only parse GMT-04:00 but not GMT-0400.
It will accept UTC-0400, however it will throw away the hours/minutes modifier and will incorrectly parse it as UTC. (This happens with any other timezone designation, except for GMT)
It will also parse -0400 correctly, so the most robust solution is probably to simply remove GMT from your date string.
The upshot of the story is that SimpleDateFormat is anything but simple.
Update: Another lesson is that I could've saved a lot of time by passing a ParsePosition object to the parse() method:
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd yyyy HH:mm:ss zzzz", Locale.ENGLISH);
Date date;
ParsePosition pos = new ParsePosition( 0 );
date = formatter
.parse("Fri Apr 01 2011 00:00:00 UTC-0400", pos);
System.out.println( pos.getIndex() );
Will print out 28, indicating that the parsing ended at character index 28, just after UTC.

Categories