I have date and time extracted from JSON in following format
2013-01-16T13:43:11
I need to convert it to local time of Pakistan and add 5 hours to that time so the result is like:
06:43
How I can achieve this?
Assuming that "2013-01-16T13:43:11" is in GMT
String s = "2013-01-16T13:43:11";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = df.parse(s);
date = new Date(date.getTime() + 5 * 3600 * 1000);
String time = new SimpleDateFormat("HH:mm").format(date);
time will be in your local timezone, so if you are in Pakistan it will be OK
You can use SimpleDateFormat as below:
String strDate = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"hh:mm");
strDate = dateFormatter.format(yourDate);
Hope it helps.
java dateFormat is not threadsafe, use joda time lib instead:
Joda time lib download
you can use withZone method to change time with specific Timezone
DateTime userTime1 = new DateTime();
DateTime eventRecordTime = new DateTime();
userTime1 = DateTime.parse("2012-07-05T21:45:00+02:00");
eventRecordTime = DateTime.parse((String) jo.get("start_time"));
DateTimeZone dtz = userTime1.getZone();
System.out.println(eventRecordTime.withZone(dtz).toString());
Related
Using this code
String twelveHourTime="06:00 PM";
public static DateTime convert12HourTimeTo24HourTime(String twelveHourTime) {
DateTimeFormatter dateTimeFormatter =
DateTimeFormat.forPattern(AppConstants.TWELVE_HOUR_TIME_FORMAT);
DateTime dateTime = dateTimeFormatter.parseDateTime(twelveHourTime);
return new DateTime().withHourOfDay(dateTime.getHourOfDay())
.withMinuteOfHour(dateTime.getMinuteOfHour());
}
I am getting this date time:
String datetime=2017-09-15T18:00:23.153+05:30
Now I want to convert it to the US time zone.
Please suggest me how to do this.
You can use SimpleDateFormat for conversion
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH24:MI");
Date date = df.parse(datetime);
Use localDateTime:
DateTime dt = new LocalDateTime(timestamp.getTime()).toDateTime(DateTimeZone.UTC);
you can use it by using TimeZone and SimpleDateFormat :-
TimeZone time = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(time);
final Date startDate = cal.getTime();
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String sDateInAmerica = sdfAmerica.format(startDate);
edDate.setText(sDateInAmerica);
So I'm getting some date objects from a web server, I know that the server has the time in GMT +1 (Berlin), how can I convert the date object, to the current phone timezone date object?
Most of the questions on stackoverflow are only about formatting within a timezone, but not actually converting like this.
I've tried this
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
calendar.setTime(timeFromServer);
Calendar calendar2 = new GregorianCalendar(TimeZone.getDefault());
calendar2.setTimeInMillis(calendar.getTimeInMillis());
WHen I print, calendar2.getTime().toString() and timeFromServer.toString() will be the same;
I used Joda time and it works. You can try with Joda time. This method will convert time from server to display format time below and change to the relevant local time
public static final String SERVER_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DISPLAY_POST_FORMAT = "HH:mm dd/MM/yyyy";
public static String convertDateStrToDisplayFormat(String timeFromServer) {
DateTimeFormatter serverFormatter = DateTimeFormat.forPattern(Constants.SERVER_FORMAT);
serverFormatter.withZone(DateTimeZone.forID("Europe/Berlin"));
DateTime dateTime = DateTime.parse(timeFromServer, serverFormatter);
DateTimeFormatter pointTimeFormatter = DateTimeFormat.forPattern(Constants.POINT_TIME_FORMAT);
pointTimeFormatter.withZone(DateTimeZone.forID("Europe/Berlin"));
return pointTimeFormatter.print(dateTime)
}
I use this to convert the date from the server and convert it to current phone timezone date object
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat deviceFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
deviceFormat.setTimeZone(TimeZone.getDefault());
Date utcDate ;
Date deviceDate ;
utcDate = sourceFormat.parse(event_date);
deviceDate = deviceFormat.parse(utcDate.toString());
event_date is a String that has the server date. After this you have your converted Date on deviceDate.
java.util.Date does not use timezone, so when you try to print the string representation of the following date objects using method Date#toString(), the results are the same:
calendar2.getTime().toString()
timeFromServer.toString()
In order to test the string representation correctly with timezone, you need to use SimpleDateFormat:
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
f.setTimeZone(calendar.getTimeZone());
// Date correctly printed with timezone:
System.out.println(f.parse(calendar.getTime()));
However, your conversion written in the question is correct, here's how I tested it using JUnit:
#Test
public void testDateConversion() throws ParseException {
String serverText = "2017-03-02T11:54:30.207+01:00";
SimpleDateFormat serverFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
serverFmt.setTimeZone(TimeZone.getTimeZone("GMT+1"));
Date timeFromServer = serverFmt.parse(serverText);
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT-1"));
calendar.setTime(timeFromServer);
assertEquals(2017, calendar.get(Calendar.YEAR));
assertEquals(Calendar.MARCH, calendar.get(Calendar.MONTH));
assertEquals(2, calendar.get(Calendar.DAY_OF_MONTH));
assertEquals(9, calendar.get(Calendar.HOUR_OF_DAY));
assertEquals(54, calendar.get(Calendar.MINUTE));
assertEquals(30, calendar.get(Calendar.SECOND));
assertEquals(207, calendar.get(Calendar.MILLISECOND));
SimpleDateFormat currFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
currFmt.setTimeZone(calendar.getTimeZone());
System.out.printf("server_timestamp = %d, server_date = '%s', server_str = '%s'%n",
timeFromServer.getTime(),
serverFmt.format(timeFromServer),
timeFromServer.toString());
System.out.printf("current_timestamp = %d, current_date = '%s', current_str = '%s'%n",
calendar.getTime().getTime(),
currFmt.format(calendar.getTime()),
calendar.getTime().toString());
}
Result:
server_timestamp = 1488452070207, server_date = '2017-03-02T11:54:30.207+01:00', server_str = 'Thu Mar 02 11:54:30 CET 2017'
current_timestamp = 1488452070207, current_date = '2017-03-02T09:54:30.207-01:00', current_str = 'Thu Mar 02 11:54:30 CET 2017'
See also:
SimpleDateFormat (Java Platform SE 7)
I have a unix timestamp. I wanted to convert into hours,min and seconds.I wanted to acheive it in java.I tried this .But I am not sure how do i have to concatenate it to hours+min+sec
int day = (int)TimeUnit.SECONDS.toDays(timeStamp);
long hours = TimeUnit.SECONDS.toHours(timeStamp) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(timeStamp) - (TimeUnit.SECONDS.toHours(timeStamp)* 60);
long second = TimeUnit.SECONDS.toSeconds(timeStamp) - (TimeUnit.SECONDS.toMinutes(timeStamp) *60);
thanks,
Ramya.
You can use the Calendar class for this. You can format the time using SimpleDateFormat
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
Date date = new Date ();
date.setTime((long)unix_time*1000);
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a",Locale.ENGLISH);
System.out.println(df.format(date));
I want to get the difference in seconds between end_date_time and system_date_time using JAVA, for example:
if
end_date_time = 2015-02-21 13:00:00
system_date_time = 2015-02-20 13:00:00
then
difference = 86400
Please tell how can I do this?
If you have a Date type already, it's as simple as:
(System.currentTimeMillis() - myDate.getTime()) / 1000;
If you have a string and need to convert it to a date, you use a SimpleDateFormat instance:
String dateString = "2015-02-21 13:00:00" // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
If you're using Java8, this is much easier:
String dateString = "2007-12-03T10:15:30.00Z";
Instant.now().until(Instant.parse(dateString), ChronoUnit.SECONDS);
Your dateString must be parseable in the ISO format, though:
"yyyy-mm-ddThh:mm:ss.SZ"
I am working on Java. I have a date for example: 20120328.
Year is 2012, month is 03 & day is 28.
I want to convert it into yyyy-MM-dd format.
I have tried it but it is not working.
How to do it?
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
If you want other than "MM/dd/yyyy" then just change the format in SimpleDateFormat
Find some examples here.
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");
Take a look here. You will need to use the SimpleDateFormat class.
First you create the format using substring like
String myDate = "20120328";
String myConvertedDate = myDate.substring(0,4) + "-" + myDate.substring(5,6) + "-" + myDate.substring(7);
This produces the date as 2012-03-28
Then use simple date format to convert that into date if required.
You can use this one for Date formatting
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
And this one for getting TimeStamp
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Timestamp timestamp =timestamp=new Timestamp(System.currentTimeMillis());
String ourformat = formatter.format(timestamp);