This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 6 years ago.
I tried to convert current date is in "UTC" timezone format
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
DateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dat = sdf.format(cal.getTime());
System.out.println("In string format" + dat);
System.out.println(("Date After parses " + (Date) sdf.parse(dat)));
Below is op:
In string format Mon Apr 11 09:57:12 +0000 2016
Date After parses Mon Apr 11 15:27:12 IST 2016
When i tried to convert date(Parse) in Date Format from String,I am getting the date is in Current date time and also in IST format.
Try the solution explained in this post
Basically he gets the Calendar instance with the timeZone set as "UTC"
Here is the code:
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTime(new Date());
DateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
Related
This question already has answers here:
I want to get Year, Month, Day, etc from Java Date to compare with Gregorian Calendar date in Java. Is this possible?
(8 answers)
Closed 7 years ago.
I know these type of question asked lot's of time on lot's of website.
Till I not got solution that's why I am putting this question here.
I want year, month, date, date and time from date format
Tue, 12 Jan 2016 09:40:07 GMT
I am getting this date format form HttpResponse header and need year, month, date, date from this date format.
Following code I am using but not getting real value:
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date date;
try {
date = dateFormat.parse(header.getValue());// here we are getting date in format "Tue, 12 Jan 2016 09:40:07 GMT"
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("YEAR: " + calendar.YEAR);
System.out.println("MONTH: " + calendar.MONTH);
System.out.println("DATE: " + calendar.DATE);
} catch (ParseException e) {
e.printStackTrace();
}
I tried this also:
1.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
2.
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
But all the above code give following result:
YEAR: 1
MONTH: 2
DATE: 5
You are using calendar.YEAR, calendar.MONTH, and calendar.DATE the Calendar class contains static YEAR, MONTH and DATE variables so it print value of these variable i.e 1,2 and 5.
Instead of it use this:
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
Set timezone to Calendar instance to GMT.
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date date;
try {
date = dateFormat.parse(header.getValue());// here we are getting date in format "Tue, 12 Jan 2016 09:40:07 GMT"
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); // set correct timezone to calendar
calendar.setTime(date);
System.out.println("YEAR: " + calendar.YEAR);
System.out.println("MONTH: " + calendar.MONTH);
System.out.println("DATE: " + calendar.DATE);
} catch (ParseException e) {
e.printStackTrace();
}
This question already has answers here:
Calendar.getInstance(TimeZone.getTimeZone("UTC")) is not returning UTC time
(7 answers)
Closed 8 years ago.
I'm trying to get an instance of Date with UTC time using the following code:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date now = cal.getTime();
that looks so simple, but if I check the values at IntelliJ's debugger, I get different dates for cal and now:
cal:java.util.GregorianCalendar[time=1405690214219,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=199,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=30,SECOND=14,MILLISECOND=219,ZONE_OFFSET=0,DST_OFFSET=0]
now:Fri Jul 18 10:30:14 BRT 2014
as you can see, cal is 3 hours ahead of now... what am I doing wrong?
Thanks in advance.
[EDIT] Looks like TimeZone.setDefault(TimeZone.getTimeZone("UTC")); before the code above does the job...
This question has already been answered here
The System.out.println(cal_Two.getTime()) invocation returns a Date from getTime(). It is the Date which is getting converted to a string for println, and that conversion will use the default IST timezone in your case.
You'll need to explicitly use DateFormat.setTimeZone() to print the Date in the desired timezone.
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
Edit To convert cal to date
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
System.out.println(year);
Date date = new Date(year - 1900, month, day); // is same as date = new Date();
Just build the Date object using the Cal values. Please let me know if that helps.
Try using a date formatter and set the time zone to UTC.
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
I want to convert the timestamp Wed, 20 Feb 2013 11:41:23 GMT to 2013-02-20T11:41:23Z . How can I do this? I want to ISO-8601 in UTC format(2013-20-02T04:51:03Z).
My code is below
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String strDate = date.toString();
Date dt = formatter.parse(strDate );
System.out.println("Date " +dt);
Output is:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed Feb 20 03:50:03 PST 2013"
Its your format what is wrong, use:
"EEE MMM dd HH:mm:ss Z yyyy" that is what is comming out of the exception, in the question you have "EEE, dd MMM yyyy HH:mm:ss Z" for Wed, 20 Feb 2013 11:41:23 GMT
Try:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss Z yyyy");
String strDate = date.toString();
Date dt = null;
try {
dt = formatter.parse("Wed Feb 20 03:50:03 PST 2013");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date " + dt);
System.out.println(new Timestamp(new Date().getTime()));
Consider using Joda Time, which has built in support for parsing and outputting ISO-format date strings.
new DateTime(DateTimeZone.UTC).toString()
You need 2 DateFormatters, one for parsing and one for output. You have the one for output.
EDIT: Output works like this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String output = formatter.format(new Date());
System.out.println("Date " + output);
You already have a Date object. You just need to format it to String.
formatter.format(date) should give you the desired result if the pattern in the SimpleDateFormat constructor is valid.
With the above implementation your code looks like this:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String dt = formatter.format(date);
System.out.println("Date " + dt);
Which results in an output like - Date 2013-02-20T17:39:45Z.
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setTimeZone (TimeZone.getTimeZone ("UTC"));
Date date = new Date ();
System.out.println ("Date is: " + format.format (date));
String dateString="2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
i get output as follows:
Converted string to date : Tue Jan 09 00:03:00 IST 2001
What is wrong with my code?
Use MM instead of mm for months - mm means minutes, not months.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
See the API documentation of SimpleDateFormat.
I create a calendar object from a string date I receive.
Calendar cal = Calendar.getInstance();
String date = "example Mon, 22 Oct 2012 23:58:31 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
sdf.setTimeZone (TimeZone.getTimeZone ("PST"));
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("date "+sdf.format(cal.getTime()));
This date converts and prints the gmt time to pst with no problem.
I then loop through an array of the elements object and add seconds
int offset = element.getSeconds;
cal.add(Calendar.SECOND, offset);
System.out.println("times cal offset: " + cal.getTime());
This cal now prints the server time (which is eastern time) and not the converted gmt that is printed above. Does something bypass the cal created from the string gmt date when seconds are added?
Thanks!
This because you don't use SDF with setted GMT timezone for printing in second time.