This question already has answers here:
Convert Date/Time for given Timezone - java
(16 answers)
Closed 6 years ago.
I would like to convert a date given in the UTC format to a date in the CET format.
The problem is that I need to add or subtract hours accordingly.
Example:
Date = "2015-07-31 01:14:05"
I would like to convert it to German date (adding two hours):
2015-07-31 03:14:05"
My code:
private static Long convertDateFromUtcToCet(String publicationDate) {
//"2015-07-31 01:14:05"
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = null;
try {
date = simpleDateFormat.parse(publicationDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTime(date);
Date givenDate = calendar.getTime();
System.out.println("Original UTC date is: " + givenDate.toString());
TimeZone timeZone = TimeZone.getTimeZone("CET");
calendar.setTimeZone(timeZone);
Date currentDate = calendar.getTime();
System.out.println("CET date is: " + currentDate.toString());
long milliseconds = calendar.getTimeInMillis();
return milliseconds;
}
This prints:
Original UTC date is: Sat Jan 31 01:14:05 IST 2015
CET date is: Sat Jan 31 01:14:05 IST 2015
First of all, your pattern string is wrong. It's yyyy-MM-dd, not yyyy-mm-dd.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
To parse with a given time zone, set it with:
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Try using:
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy-mm-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"
+ gmtTime.toString() + "," + gmtTime.getTime());
// Convert UTC to Local Time
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
+ fromGmt.toString() + "-" + fromGmt.getTime());
Related
I am trying to convert GMT to IST.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS");
Date c= sdf.parse("2017-03-31T10:38:14.4723017Z");
Date date = new Date();
DateFormat istFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone istTime = TimeZone.getTimeZone("IST");
istFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(istTime);
System.out.println("GMT Time: " + istFormat.format(c));
System.out.println("IST Time: " + gmtFormat.format(c));
My output is
GMT Time: 31/3/17 6:26 AM
IST Time: 31/3/17 11:56 AM
But my actual output should be
GMT Time: 31/3/17 5:08 AM
IST Time: 31/3/17 10:38 AM
What is wrong with my code?
Milliseconds (SSS) can only be three digits. On more than that, the date rolls over - e.g. 10:38:14.1000 becomes 10:38:15.000. Add a couple of million milliseconds... and you get the behaviour that you're seeing now.
Try this.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date c = sdf.parse("2017-03-31T10:38:14.472Z");
System.out.println(c);
DateFormat istFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone istTime = TimeZone.getTimeZone("IST");
istFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(istTime);
System.out.println("GMT Time: " + istFormat.format(c));
System.out.println("IST Time: " + gmtFormat.format(c));
Have you tried changing the format to "yyyy-MM-dd'T'HH:mm:ss.sssssssZ"?
Also, when you type Z in your Date object, you should write the time zone designator, for example, "2017-03-31T10:38:14.4723017+01:00".
Try use this format "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"
i have the following code that converts GMT time to local time, I took it from an answer here on StackOverflow, the problem is that this code return a false value of GMT time .
My GMT Time is : +3, but the code is using +2, it takes the GMT time from my device i guess, and my device's time is +3 GMT .
Here's the code :
String inputText = "12:00";
SimpleDateFormat inputFormat = new SimpleDateFormat
("kk:mm", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("kk:mm");
// Adjust locale and zone appropriately
Date date = null;
try {
date = inputFormat.parse(inputText);
} catch (ParseException e) {
e.printStackTrace();
}
String outputText = outputFormat.format(date);
Log.i("Time","Time Is: " + outputText);
The log returns : 14:00
This is related to the date on which you are doing the conversion.
You are only specifying the hours and minutes, so the calculation is being done on January 1 1970. On that date, presumably, the GMT offset in your timezone is just 2 hours.
Specify the date too.
SimpleDateFormat inputFormat =
new SimpleDateFormat("kk:mm", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = inputFormat.parse("12:00");
System.out.println("Time Is: " + outputFormat.format(date));
Ideone demo
Output:
Time Is: 1970/01/01 12:00
Additional code to show Daylight Savings Time / Summer Time impact:
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat finlandFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
finlandFormat.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));
SimpleDateFormat plus3Format = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
plus3Format.setTimeZone(TimeZone.getTimeZone("GMT+3"));
Date date = gmtFormat.parse("1970/01/01 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));
date = gmtFormat.parse("2016/04/22 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));
Output:
Time Is: 1970/01/01 12:00
Time Is: 1970/01/01 14:00 EET <-- Eastern European Time
Time Is: 1970/01/01 15:00 GMT+03:00
Time Is: 2016/04/22 12:00
Time Is: 2016/04/22 15:00 EEST <-- Eastern European Summer Time
Time Is: 2016/04/22 15:00 GMT+03:00
I'm trying to change the time that I get (in CEST/CET) to GMT to store it in my database. BUT when I parse the date in CEST to GMT, instead of subtracting 2, it adds 2 hours!
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); //My locale is CEST
Date dateOfBooking = formatter.parse(bookedDate + " " + bookedDateTime); //Here the time is 10:09
formatter.setTimeZone(TimeZone.getTimeZone("GMT")); // Timezone I need to store the date in
dateOfBooking = formatter.parse(bookedDate + " " + bookedDateTime); // Here the time is 12:09
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
bookedDateTime = timeFormat.format(dateOfBooking);
Can anybody explain why? I've tried setting my local timezone to different ones and it always work the other way, subtracting instead of adding and viceversa.
You are parsing the date again as GMT. (which, when printed as CEST, or your locale timezone, will add + 2 hours)
What you actually want is to print out the already parsed date as GMT:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); //My locale is CEST
Date dateOfBooking = formatter.parse(bookedDate + " " + bookedDateTime); //Here the time is 10:09
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
bookedDateTime = timeFormat.format(dateOfBooking);
System.out.println(bookedDateTime);
Basicly you have to set the GMT zone in your timeFormat that you use to create the time string, not the formatter that you use for parsing
I have created a web service which returns the date of an event which is initially captured by the getDate() function. I want the date returned by this function (something along this format : 2013-05-17 14:52:00.943) to be parsed and shown to the user in the DD-MM-YYYY format.
Any suggestions? I haven't found any solution along this direction yet.
I have tried this code and it's work fine for me,Please Try my code below: Please upvote to Tarun also coz he gave almost right answer.just he did mistake that he passes cal.getTime() method instead of pDate
String formatDate = p.format(pDate);
and second mistake in format like"DD-MM-YYYY" but actual format is:
"dd-MM-yyyy" not "DD-MM-YYYY"
I have done changes in it and modify it.
String dateStr = "2013-05-16 14:52:00.943";
SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); // your web service format
Date pDate = c.parse(dateStr);
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy"); // your required format
String formatDate = p.format(pDate); // convert it in your required format
SimpleDateFormat formatter = new SimpleDateFormat("EEEE"); // Day format as you want EEE for like "Sat" and EEEE for like "Saturday"
String Day = formatter.format(pDate); // This will give you a day as your selected format
System.out.println("Date & Day>>>"+formatDate+" "+Day);
// For GMT format your format should be like this: "2013-05-16 14:52:00.943 GMT+05:30"
// Give it to me in GMT time.
c.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
System.out.println("GMT time: " + c.format(pDate));
Output:
Date & Day>>>16-05-2013 Thursday
GMT time: 2013-05-16 02:52:00.943 Greenwich Mean Time
Joda time:
you can download 2.0 jar file of joda time from here:
DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter
.parseDateTime("2013-05-17T16:27:34.9+05:30");
Date date2 = jodaParsed.toDate();
System.out.println("Date & Day:" + jodaParsed.getDayOfMonth() + "-" + jodaParsed.getMonthOfYear() + "-" + jodaParsed.getYear() + " " + jodaParsed.getHourOfDay() + ":" + jodaParsed.getMinuteOfHour()+" "+jodaParsed.dayOfWeek().getAsText());
output:
Date & Day:17-5-2013 16:27 Friday
Hope it will work for you.
String dateStr = "2013-05-17 14:52:00.943";
SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
Date pDate = c.parse(dateStr);
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy");
String formatDate = p.format(pDate);
You can use Joda Time if you have colon in time offset.
DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter.parseDateTime("2013-05-17T16:27:34.9+05:30");
Date date = jodaParsed.toDate();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.get(Calendar.DATE));
More info about joda can be found here.
Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.
Example:
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("05/18/2013");
System.out.println(format2.format(date));
Output:
11-05-2013
Edit:
Calendar cal = Calendar.getInstance();
cal.setTime(specific_date);
int dayOfMonth = cal.get(Calendar.DAY_OF_WEEK);
String dayOfMonthStr = String.valueOf(dayOfMonth);
How to store date and time in different variable from this type of date in java
Aug 29 2011 2:24PM
i want store date = 8/29/2011 and time = 2:24PM
hows it possible?
Try This
String inputDateInString = "Aug 29 2011 2:24PM";
DateFormat formatter = new SimpleDateFormat("MMM dd yyyy h:mmaa");
try {
Date dateObject = formatter.parse(inputDateInString);
String date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
String time = new SimpleDateFormat("h:mmaa").format(dateObject);
} catch (ParseException e) {
e.printStackTrace();
}
Here is an example using the SimpleDateFormat class
Date today = new Date();
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
DateFormat year = new SimpleDateFormat("yyyy");
DateFormat month = new SimpleDateFormat("MM");
DateFormat day = new SimpleDateFormat("dd");
System.out.println("today is: " + format.format(today));
System.out.println("The year is: " + year.format(today));
System.out.println("The month is: " + month.format(today));
System.out.println("The day is: " + day.format(today));
Actually, your question is not very clear, but I assume you want to get day, month, etc fields from the date in String. So use SimpleDateFormat to achieve this.
String date="Aug 29 2011 2:24PM";
DateFormat format = new SimpleDateFormat("MMM dd yyyy HH:mm");
Date dt= format.parse(date);
Calendar calendar=Calendar.getInstance();
calendar.setDate(dt);
int d= calendar.get(Calendar.DAY_OF_MONTH);
If you want to get hold of the Year, Month, Week, Day etc, the Calendar class would be what you are looking for.
Calendar c = Calendar.getInstance();
System.out.println("Year: " + c.get(Calendar.YEAR));
System.out.println("Month: " + c.get(Calendar.MONTH));
....