This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,
Related
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.
I have time on device 11:34
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'hh:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");// I PARSE THIS DATE!!!
RESULT IS :
date_start:
Wed Mar 12 00:00:00 Восточноевропейское время 2014
BUT SHOULD BE:
Wed Mar 12 12:00:00 Восточноевропейское время 2014
HOW to solve it?
To get 24h format use HH not hh. In 12h format hours can be in rage 0-11, which makes 12 overflow to 0.
Use
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
use 24 hour date format
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
First of all take a look about patterns of Simpledatrformat. where it clearly shows H is for (0-23).
Reference for Date Format Pattern Syntax
so you should change your code like below.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");
System.out.println("now time is.." + date_start);
OR
Use this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
Thanks.. use above code to parse correctly!!
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
This question already has answers here:
Is java.util.Date using TimeZone?
(5 answers)
Closed 7 years ago.
String = 26/8/2013 15:59;
I want to convert this date into GMT, however after applying the below code, I get the EEST time rather than the GMT.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy h:m");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.parse(newDate);
Log.i(tag, df.parse(newDate).toString());
Output :
Mon Aug 26 18:59:00 EEST 2013
Whats wrong ?
Your parsing is correct, the different is just for your locale time zone that is used to display when you are making toString(). I just used formatted output to demonstrate the correct format . Here is the details example:
final String time = "26/8/2013 15:59";
TimeZone timeZone = TimeZone.getTimeZone("UTC");
final String REQUEST_DATE_FORMAT = "dd/MM/yyyy h:m";
DateFormat format = new SimpleDateFormat(REQUEST_DATE_FORMAT);
Date localDate = format.parse(time);
// localDate.toString()
// PRINT. Mon Aug 26 15:59:00 EEST 2013
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(localDate);
format.setTimeZone(timeZone);
final String utcTime = format.format(cal.getTime());
// PRINT. 26/08/2013 12:59
Nothing's really wrong. You are successfully parsing the datetime string interpreted as UTC timezone.
When printing it to log, you get what you ask for - Date.toString() returns the date formatted to current locale settings which include the timezone. The difference between UTC and EEST is 3 hours.
If you want to to format it to display some other timezone, pass it though format() of a SimpleDateFormat that is configured to the timezone you want.
I think you should use the below approach:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
I have this piece of simple code:
SimpleDateFormat sqlFormatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
I get this output:
2012-03-09 12:00:00
Sun Jan 01 12:00:00 EST 2012
I know is supposed to be simple, but I am hoping someone can quickly see what I am missing.
I think your pattern is a little off. I'm suprised you're not seeing an IllegalArgumentException. Try using the following pattern with lower case y's and see if that resolves your issue:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Corrected code here:
SimpleDateFormat sqlFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println(temp);
Date last = sqlFormatter.parse(temp);
System.out.println(last);
You should have SimpleDateFormat instead of SimpleDateFormatter and for years you give yyyy instead of YYYY.
Once I corrected your format String - Y is not allowed, you need y - (and the typo already mentioned) it worked fine:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
>2012-03-09 12:00:00
>Fri Mar 09 12:00:00 EST 2012
You need to use 'yyyy' and not 'YYYY'
Here is the output
2012-03-09 12:00:00
Fri Mar 09 12:00:00 IST 2012
for the pattern
yyyy-MM-dd HH:mm:ss