I am using SimpleDateFormat class to get current year like this
SimpleDateFormat currentYear = new SimpleDateFormat("YYYY");
Date thisYear = new Date();
String stringThisYear = currentYear .format(thisYear );
It is working fine in here(India) ,but it crashed when it run on Malaysia by other people.After lot of debugging and searching ,i found that it is happening due to no mention of locale.So I changed my code to this
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.ENGLISH);
Still it is crashing, so I use calendar instance to get current year like this
int thisYear=Calendar.getInstance().get(Calendar.YEAR);
But i want to know what is issue with simpleDateFormat as most of my code is already has simpleDateFormat class.So I want to know the reason for crash,if anyone has solution,Please help me.Thank you
It's because you must use "yyyy" instead of "YYYY"
On old versions of Android, using "YYYY" makes your app to crash. Nothing to do with the country/Locale
"Y" stands for week year and not year, as specified in the Javadoc
EDIT 2
More information here
yyyy is the pattern string to identify the year in the
SimpleDateFormat class.
Java 7 introduced YYYY as a new date pattern to identify the date week
year.
An average year is exactly 52.1775 weeks long, which means that
eventually a year might have either 52 or 53 weeks considering
indivisible weeks.
Using YYYY unintendedly while formatting a date can cause severe
issues in your Java application.
As mentioned by #OleV.V., the format Y is only supported on Android API 24+. Using any versions below API 24 makes the app to crash.
Try to use default locale for the device like this
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.getDefault());
I am facing the same issue using this it's working fine.
SimpleDateFormat formatter = new SimpleDateFormat("YYYY",Locale.US);
Related
I'm trying to get the date from DateChooserCombo as follows
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
String date = sdf.format(dateChooser.getDate());
But the method getDate() gives me error (illegal forward reference). I have also tried with getSelectedDate() but it's the same. What can I do?
Anyway I'm using Apache Netbeans 12.1 and the date picker should be this one:
https://github.com/vadimig/jdatechooser
Thanks.
I downloaded the JDateChooser code from the link you provided in your question. There is no getDate() method in class datechooser.beans.DateChooserCombo. There is a getSelectedDate() method which returns an instance of class java.util.Calendar.
Also, according to the documentation for class java.text.SimpleDateFormat, the pattern YYYY-MM-DD is a valid pattern but I don't think it's the pattern that you want. D means day in year which means that 27th February is the 58th day of the year. You probably want d. Similarly, Y means Week year whereas you probably wanted y.
So, in order to get a string representation of the date that the user selected from the DateChooserCombo, you probably want the following code.
DateChooserCombo dcc = new DateChooserCombo(); // or however you create and configure it
Calendar cal = dcc.getSelectedDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(cal.getTime());
By the way, it appears that JDateChooser development stopped seven years ago. Perhaps consider using JavaFX which has a DatePicker component which works with Java's date-time API.
I am baffled why this doesn't work. No matter what I change the day to, it prints out the same thing. What is going on here?
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
String s = "11/22/2000";
Date d = sdf.parse(s);
System.out.println(d.toString());
Output: Sun Dec 26 00:00:00 CST 1999
You're using YYYY which is the week-year instead of the "normal" year. That's usually only used with day-of-week and week-of-year specifiers.
The exact behaviour here would be hard to explain - feasible, but not terribly helpful. Basically the solution is "don't do that". You want a format string of MM/dd/yyyy instead - note the case of the yyyy.
(As a side note, if you can possibly use java.time.* from Java 8 or Joda Time, you'll have a lot better time in Java with date/time issues. It wouldn't affect this particular issue, but it's generally a good idea...)
Use
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy")
instead of
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
I call a service which returns GMT dates. Its been working fine since November, but now with daylight savings time active, its failing. Here's a sample date from non-daylight savings time:
2011-12-07T15:50:01Z
And one from today (in daylight savings time):
2012-03-26T11:05:01+01:00
Previously I've been using this pattern:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.UK);
But its failing on the second date above with a ParseExcepton ("Unparsable date..."). So, can one pattern be used for both, and if so what is it? If I can't use one pattern for both, what is the correct pattern for the second date above?
It shouldn't make a difference, but if it does this is in use on the Android platform.
It definitely makes a difference that you're using Android, as it would make a difference in this case if you were using Java 5/6 or 7.
The pattern you're using specifies a literal 'Z' (also 'T') to be parsed. It is not parsing a timezone. You need to drop the single-quotes from around the 'Z' to start parsing an actual time-zone.
According to the Android JavaDoc, it is unclear whether a capital Z will even work in this case, as the format of the hours/minutes is pretty specific. I don't know enough about the Android SDK to confirm, but the colon definitly makes a difference in standard Java.
The new ISO8601 time zone pattern is covered by the X pattern specifier which is introduced in Java 7.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.UK);
If you're still on Java 6 or older, then yes it may make difference. You'll need either to parse it (partially) yourself or to grab Joda Time.
In case you use java6, you will have to identify the patterns and then apply the formater
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String date2Str="2011-12-07T15:50:01Z";
Date date2 = df.parse(date2Str);
System.out.println(date2.toString());
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
String date1Str="2012-03-26T11:05:01GMT+01:00";
Date date1 = df2.parse(date1Str);
System.out.println(date1.toString());
This question already has answers here:
Convert GMT to IST in java?
(5 answers)
Closed 6 years ago.
I am trying to convert time zone from UTC to GMT in java. I have tried several times and even used your guided method too. I am getting my output with correct timing in GMT format but along with "PDT 2012" written with it. Why so..?? I have tried hundreds of methods but can't get rid of it.
Please help me.
Thanks
For all Date / or DateTime related operations in Java I would recommend to use JodaTime Library
It is very useful to use Date/time with different point of views (calendar, timezone) and for computation as well: adding/substracting months, years, days and so on...
Since Java 8, an equivalent (improvement) of JodaTime is included in the JDK under the new package java.time (JSR-310) and no more needed to add it as dependency.
The author of JodaTime explains in his blog the difference between JodaTime and JSR-310.
Perhaps the following will be a starting point. It converts your current date to GMT:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());
TimeZone currentTimeZone = cal.getTimeZone();
int offset = currentTimeZone.getOffset(cal.getTimeInMillis());
Date adjustedTime = new Date(cal.getTimeInMillis() - offset);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.format(adjustedTime));
A couple of notes:
You are probably not able see the PST change to UTC because you don't set the timezone on the date format
You shouldn't really use the abbreviations like "GMT" anymore. It is better to use the full name in the id field.
You'll have to be a bit more creative if you happen to run the above code on a system that has its default time already set to GMT.
Somehow I am being reported a issue, in which following code provides date in future.
The timezone used is GMT+01:00.
The numberOfDays is non negative integer.
The intention of this code is reduce the number of days from current date.
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yy",Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -numberOfDays);
Date date = calendar.getTime();
String dateStr= formatter.format(date);
System.out.println("Date : "+dateStr);
I am not able to reproduce this on my machine.
Does the Locale affect the TimeZone?
I tried to correlate to Why does a new SimpleDateFormat object contain calendar with the wrong year?, and Strange problem with timezone, calendar and SimpleDateFormat but in vain.
Please help me understand and rectify this issue.
Well, two possibilities I can think of off the top of my head:
The system date on the client machine is incorrect, so the calendar starts with a date in the future
If numberOfDays is negative, it will obviously push the date into the future
The Locale isn't directly related to the time zone - they're independent, although obviously a machine with a French locale is likely to be in a French time zone etc.
Personally I would avoid using Date/Calendar entirely and use Joda Time as a much nicer date and time API, but that wouldn't help with either of the ideas I gave above...