I have to convert the time which i get as input and convert to current time zone which had been set in device and display.
I tried some extent but the time not changed while converting.Have posted my code..
String dateandtime="3/14/2013 2:38am" //EDT time
Calendar cal = Calendar.getInstance();
TimeZone z = cal.getTimeZone();
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy hh:mma");
format1.setTimeZone(z);
cal.setTime(format1.parse(dateandtime));
The value of "dateandtime" wil be EDT timezone and should convert to any of current time zone.. I receive the same date and time as output..
Related
I convert json date to human readable date but it shows less one then actual date. I
used this code to convert it:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Long timeInMillis = Long.valueOf(AttendanceModelList.get(position).getEmpdate());
calendar.setTimeInMillis(timeInMillis);
Date date=new Date(timeInMillis);
viewHolder.textemployeedate.setText(df.format(date));
Please help
You say as summary:
Your calendar date is one day less than expected when you try to
interprete a global timestamp of type java.util.Date as calendar
date.
This phenomenon can happen due to timezone effects or midnight change. Before viewing the technical solution, you have to ask yourself:
What is your default (system) timezone using TimeZone.getDefault()?
Do you run your code on a server which has not the expected timezone?
In which timezone do you wish to view the calendar date? (the timezone associated with your expected "actual" date)
How to specify the timezone?
java.util.Date d = ...; // from your JSON-timeInMillis?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tz = "Asia/Kolkata"; // or any other valid tz id
sdf.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(sdf.format(d));
How can I change the timezone, when I use this code to get the date?
Calendar c = Calendar.getInstance();
String date = org.apache.http.impl.cookie.DateUtils.formatDate(c.getTime());
This returns me the date in my timezone (GMT+00:00) but I need the CET time.
Use the setTimeZone method on your calendar to set the time zone to CET.
c.setTimeZone( "Europe/Amsterdam" );
I need to show the device time in the chat messenger but it shows only the zone time.If I change the device time both time will be not same.
I want to display the current device time in Timestamp I used joda.Please check my code.
DateTime createdAt = message.getCreatedAt();
DateTimeZone dateTimeZone= DateTimeZone.getDefault();
DateTimeFormatter fmtTimeBubble = DateTimeFormat.forPattern(LBUtil.TIME_AM_PM_FORMAT).withZone(dateTimeZone);
viewHolder.dateTime.setText(fmtTimeBubble.print(createdAt));
Use below method for getting time according to any timezone, you have to pass 3 parameters as :
System Current time in millis.
Device timezone
And, format of time like (MMM dd, hh:mm a)
public static String getFormatedTime(long dateTime,String timeZone, String format){
String time = null;
try{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(dateTime);
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
time = sdf.format(cal.getTime());
}
catch(Exception e){
//logger.log(Level.SEVERE,"\n ERROR**********, Exception during get formated time: "+e+"\n");
}
return time;
}
For Calling this method, you have to get device timezone programmatically :
Calendar calendar = Calendar.getInstance();
TimeZone zone = calendar.getTimeZone();
String timeZone = zone.getID();
Now Call this method :
String msgAtTime = getFormattedTime(System.currentTimeInMillis(), timezone, "MMM dd, hh:mm a");
Now, set return String in textview.
msgTextVies.setText(msgAtTime);
I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.
I want to set an alarm at a particular date and time . iam getting my date and time with the webservice.i have parsed and splitted the date and time and used SimpleDateFormat and now i want to put this date and time in [alarmManager.set(AlarmManager.RTC_WAKEUP,dt, pendingIntent );] but my alarm doesnot work on the given time
String str_date= hr+":"+min+":"+sec+" "+dat+"/"+mon+"/"+year;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
ParsePosition position = new ParsePosition(0);
java.util.Date stringToDate = sdf.parse(str_date, position);
dt = stringToDate.getDate();
Please help
thanks in advance
The time you should pass to Alarm.set is a long and getDate (which is deprecated) returns an int.
Try with getTime()
AlarmManager use time in millesecond unit at UTC time zone...
So time parsing have to take the timezone into account
So after SimpleDateFormat new instance you may set the UTC timezone
example :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
then your parsing will be ok and compatible for the AlarmManager service.