I am programmatically trying to set the timezone to Indian Standard Time (IST) in Android, but nothing seems to work!
Here's the code snippet:
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
// TimeZone.setDefault(TimeZone.getTimeZone("GMT +5:30"));
// TimeZOne.setDefault(TimeZone.getTimeZone("IST"));
Calendar c = Calendar.getInstance();
System.out.println(s.format(c.getTime()));
Rather than changing the default time zone (which you're doing after creating the SimpleDateFormat) you should just set the time zone of the SimpleDateFormat:
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
s.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(s.format(new Date()));
Related
I want to get current time specifically in English to save it in Database,
to get Current time i use function
private String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
return (String) DateFormat.format(CURRENT_TIME_FORMAT, Calendar.getInstance().getTime());
}
but when i set Locale to different language it gives me current time in that language.
for example if i set
conf.setLocale(new Locale("mr"));
it gives me date in marathi. I specifically want it in English. How to do it.
And Also how to change the language of Date once it is saved.I mean if I have saved date in English and while display i want that date to be shown in some other language, how to do it.?
As ADM suggested. new function that worked
public String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
SimpleDateFormat dateFormat = new SimpleDateFormat(CURRENT_TIME_FORMAT, Locale.ENGLISH);
return dateFormat.format(Calendar.getInstance().getTime());
}
So now it always returns Date in English
This should fix your issue! Try any of these
here is a simple tutorial
For java.util.Date, just create a new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
log.d(dateFormat.format(date)); //2016/11/16 12:08:43
For java.util.Calendar, uses Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
log.d(dateFormat.format(cal)); //2016/11/16 12:08:43
For java.time.LocalDateTime, uses LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
log.d(dtf.format(now)); //2016/11/16 12:08:43
For java.time.LocalDate, uses LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
log.d(dtf.format(localDate)); //2016/11/16
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));
In my application i have edit text that will show the current date and time initially . If i change the date to some other date and when i tried to use gettext() i am still getting the current date. can anyone help. This is my code to set initial value in edit text
TaskTime = (EditText) findViewById(R.id.txtTaskTime);
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
TaskDate.setText(dateFormat.format(date));
Most of the use of date object is obsolete in Android, you should use Calendar : http://developer.android.com/reference/java/util/Calendar.html
Yes, you can get the current date because , Date date = new Date(); this function always returns the current date .
So you need to use the Calendar object to get the current date and time .
Example
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String strDate = sdf.format(cal.getTime());
Here in dateFormat you have to declare your date format . dateFormat = "dd/MM/yyyy" like .
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.