Java: Display "Europe/Stockholm" time whatever my locale is - java

I ma trying to get the output time of "Europe/Stockholm" timezone whatever my computer local is (I have currently got GMT 0 on my laptop).
TimeZone tz = TimeZone.getTimeZone("Europe/Stockholm");
Calendar cal = Calendar.getInstance(tz);
Date date = new Date();
cal.setTime(date);
cal.setTimeZone(tz);
long curSeconds = (cal.getTimeInMillis()/(long)1000);
Log.v("Karl","new Date() "+cal.getTime());
But this still outputs:
new Date() Wed Mar 04 17:33:53 GMT+00:00 2015
I would like it to display the timezone of "Europe/Stockholm" as said!

When you call cal.getTime() you get java.util.Date. java.util.Date has no time zone - it is just a point in timeline, without location. (I mean you lost your calendar timezone when you cal.getTime())
java.util.Date.toString() displays date in host timezone (Timezone.getDefault())
That was a theory. Practice:
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
df.setTimeZone(tz);
System.out.println(df.format(cal.getTime()));
You will get
Wed Mar 04 22:01:53 CET 2015

Related

Java simpleDateFormat does not parse date correctly for given Locale?

Here in my code I get the current time and than format it according to "UTC" timezone and than parsed it using the same Timezone hence converting it to a Date. The problem is that I get the date as "Wed Jul 20 13:04:51 GMT+05:00 2016" than
its formatted and parse as in the code below
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
//This formats dates as following "Wed, 20 Jul 2016 08:04:51 +0000"
Now uptil here things work fine, the time with timezone GMT+05:00 is converted to standard UTC with GMT+00:00 that shown by +0000 in the formatted date
Date setteledDate = dateFormat.parse(dateText);
Now Parsing the date as above gives the following date.
"Wed Jul 20 13:04:51 GMT+05:00 2016". The problem is this date is again in GMT+5
and the whole purpose of me getting current date formatting it and than parsing it is lost because the purpose was to get current date according to "UTC" (GMT+00:00).
Please help.
Replace Z to z in SimpleDateFormat format.
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:01:20 UTC
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:02:04 GMT+00:00
//update
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
try {
Date date = dateFormat.parse("Wed Jul 20 13:04:51 GMT+05:00 2016");
Log.e("date",date.toString());
} catch (ParseException e) {
e.printStackTrace();
}

SimpleDateFormat is not giving right time?

http://ideone.com/T5wSRV this is the link to below code
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
this is not giving correct IST time where as code below is working fine . why?
http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time.
And when you format then parse the formatted string, you still have the same date:
I commented the code with the results:
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date d = new Date();
System.out.println(d);
// Mon Mar 16 16:57:19 CET 2015
=> now in my TZ (CET)
System.out.println(dateFormatIST.format(d));
// 2015-Mar-16 21:27:19
=> now in IST TZ
System.out.println(dateFormatLocal.format(d));
// 2015-Mar-16 16:57:19
=> now in my TZ (CET)
Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
System.out.println(dateIST);
// Mon Mar 16 16:57:19 CET 2015
=> The dateIST object contains still "now", and the format is default local which is CET
Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
System.out.println(dateLoc);
// Mon Mar 16 16:57:19 CET 2015
=> same thing as above
Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
System.out.println(dateLocIST);
// Mon Mar 16 21:27:19 CET 2015
=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".
If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.

After DateTimeZone.convertLocalToUTC timezone still shows local

I use DateTimeZone.convertLocalToUTC to convert local time to UTC. The time is changed correctly, but after conversion, the timezone info still says the Original local timezone. Please refer below sample code
Date gmttime = new Date(tz.convertLocalToUTC(System.currentTimeMillis(),false));
System.out.println(gmttime.toString());
Output :
Wed Oct 16 12:58:19 IST 2013
Please note the bold value, I expected it to be UTC . Please let me know if I am missing something.
#Date.toString() will print the date in the local timezone.
Use SimpleDateFormat to print the Date formatted for a specific TimeZone:
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss:SS z");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(format.format(date));
}
Try:
final Date date = new Date();
final String ISO_FORMAT = "E MMM dd HH:mm:ss zzz yyyy";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);
System.out.println(sdf.format(date));
Output:
Wed Oct 16 08:53:50 UTC 2013
convertLocalToUTC Converts a local instant to a standard UTC instant with the same local time. http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html

Date value wrongly formatted

I am trying to convert a String DateTime value which is present in a flat file as a Date object after parsing the flat file in my code.
I have written the code to do that but when I format the date its always giving me a date more than 1 day for the specified value, some times it's adding 5:30.
Below is the code for that:
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + date);
The output for the above is
---date----Wed Aug 24 08:30:03 IST 2011
Can you please let me know whats the issue here. Is there a problem in the pattern that I am using in the SimplaDateFormat class or is there a problem with the code.
I have been scratching my head on this for a long time now.
Can you please let me know whats the issue here.
Sure. You're effectively calling date.toString(), which doesn't know anything about the SimpleDateFormat which was used to parse the original text value. A Date is just an instant in time. It has no notion of a per-instance format. Additionally, it doesn't know about a time zone. You've given a value in PDT, which was then parsed... and when you print it, it's using the system local time zone (IST). That's what Date.toString always does.
If you want to format a Date in a particular way, using a particular format in a particular time zone, call DateFormat.format.
Your system timezone is different. The output is showing IST - or Indian Standard Time, which is an 12.5h difference from PDT. The code is properly parsing the given date which is PDT (UTC -7) and printing out IST (UTC +5h30).
Java stores Dates as UTC dates. So when you parse the PDT date, Java will convert it to UTC and store it internally as a UTC timestamp. When you print, if you do not specify the timezone, it will default to the system timezone, which in your case would appear to be IST.
To specify an exact timezone, specify it in the SimpleDateFormat:
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
f.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + f.format(date));
Because you are not formatting a date. Look at the example
public static void main(String[] args){
Locale currentLocale = Locale.US;
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
Date date = null;
Date today;
try {
today = new Date();
String result = f.format(today);
System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);
date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("---date----" + f.format(date));
}
will output
Locale: en_US
Result: Tue Sep 25 19:12:38 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011
Now, you have a bit modified code
public static void main(String[] args){
Locale currentLocale = Locale.US;
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
DateFormat f2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
Date date = null;
Date today;
try {
today = new Date();
String result = f.format(today);
System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);
date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + f.format(date));
System.out.println("---date----" + f2.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
which outputs to
Locale: en_US
Result: Tue Sep 25 20:42:10 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011
---date----Wed Aug 24 06:00:03 EEST 2011
seems that SimpleDateFormat don't care about timezone even if 'z' pattern is specified. It is setting the timezone when it parses the input. That's how I can describe that a strange behavior. Then use of 'z' pattern seems obsolete and lead to unpredictable results.
so setting the TimeZone will fix the issue
f2.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

Mysql datetime to Calendar

My code:
Calendar c = Calendar.getInstance();
c.setTime(res.getDate("my_date")); //res is ResultSet
System.out.println(c.getTime());
System.out.println(res.getString("my_date"));
Output:
Thu Oct 11 00:00:00 IST 2012
2012-10-11 02:50:00.0
In calendar, it is not considering the time part. I used Calendar because I have to make some comparisons.
Why time part is omitted?
You should use something like :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
out.println(sdf.format(rs.getTimestamp("your_date_field").getTime()));
By the way, you have to create a SimpleDateFormat that fit your SQL date format.

Categories

Resources