How to get Current Date and time of Dubai in Android? - android

I am getting current date of the device, but i want the date and time of city Dubai, Any one please help me in doing this.when the activity called the date and time should be displayed,
Any help will be appriciated. thank you..

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
// => This time is in the user phone timezone, you will maybe need to turn it in UTC!
TimeZone tz = TimeZone.getTimeZone("Asia/Dubai");
format.setTimeZone(tz);
String result = format.format(parsed);

You can use TimeZone to pass to the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));

Related

DateTime Local time zone to UTC and UTC to some other selected timezone conversion in Android

I am developing an android application. In my app I stored my logged-in local date time in preference and showing in the user profile page. After some of days I changed my mobile timezone as Australia. Now I need to convert my existing local date time to Australia timezone date time and display in profile page. Is it possible. Show me the code snippet. Your help will be appreciated. Thanks in advance.
String inputText = "2017-05-17 15:50:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("MMM dd, yyyy h:mm a");
// Adjust locale and zone appropriately
Date date = null;
try {
date = df.parse(inputText);
} catch (ParseException e) {
e.printStackTrace();
}
String outputText = outputFormat.format(date);
System.out.println(outputText);
Try this code.. it may help

How to show the time and date saved in my database to the user according to their local timezone? Please see details

I'm developing an app in which I'm saving the date and time in Firebase and then retrieving it for the user to see.
What I want is I want user to see that time and date according to their local timezone. for ex - if the time and date saved in database is '2:07 PM' and 'Fri, Jun 24', then to the users in PST timezone, it should be shown as '1:37 AM' and 'Fri, Jun 24'.
Here's how I'm getting time and date:
DateFormat currentTime = new SimpleDateFormat("h:mm a");
final String time = currentTime.format(Calendar.getInstance().getTime());
DateFormat currentDate = new SimpleDateFormat("EEE, MMM d");
final String date = currentDate.format(Calendar.getInstance().getTime());
How can I achieve this?
Please let me know.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
Date myDate = simpleDateFormat.parse("DateAndTimeToBeProcessed");

Getting the current time and date in the device's defined format

How can I get the current time and date according to phone device format?
my code is
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",
Locale.ENGLISH);
String var = dateFormat.format(date));
It displays like 2PM instead of 14PM.
You can the device format data and time using
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Log.d(TAG,dateFormat.format(date)+");
Use calendar object instead of Date because it is deprecated now and by using caledar object you can get hour in 12 hours formate as
int hour = calendarObject.get(Calendar.HOUR);
for 24 formate as:
int hour = calendarObject.get(Calendar.HOUR_OF_DAY);
for further detail you can visit this
Just change "hh:mm aa" to "HH:mm aa"

displaying date in edit text android

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 .

Alarm Manager problem in time setting

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.

Categories

Resources