How can i get current time format that is set in android. Like it is 12 hrs or 24 hrs. I can easily get date and time and put format on it but i want to get current format of time from android system.
Use the following:
DateFormat.is24HourFormat(context);
Source: https://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat(android.content.Context)
If you want to get a String you should use android.text.format.DateFormat
DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(context);
String = dateFormat.format(new Date());
Related
I want to get the date of SMS in format dd-MM-yyyy and time in format HH:MM (in 24hr format) on which it was received on phone. I use the following code
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(messages[0].getTimestampMillis());
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
String finalDateString = formatter.format(calendar.getTime());
Log.i(TAG, "Date new Format " + finalDateString);
Message was recieved on 11:50PM on 26-Dec-19 at phone, but I get the result in Log "Date new Format 27/12/2019 10:50:03.000". Please notice instead of 26 its giving 27 and time is also 1 hr less, instead of 11 its 10.
Is this a normal behaviour? Do I have to subtract 1 day from date to get correct data and add 1 hr in time? Will this always work correctly? Do I have to specify timezone to get correct data? Please advise as I got confused with ths.
Try as follow
formatter.timeZone = TimeZone.getTimeZone("UTC")
I'm retrieving hours data for places from a service (Factual). It comes to me in 24-hour format and I need to display it in 12-hour format. The data for a specific day comes like this:
"sunday\":[[\"12:00\",\"21:30\"]]
I can successfully retrieve the hours from the JSON. Then, using SimpleDateFormat, I can parse the string to a Date object. But, then I can't figure out how to convert them to 12;-hour format so that I can display them as "12:00 - 9:30" or "12:00pm - 9:30pm" rather than "12:00 - 21:30".
How can I go about doing this? Thanks!
EDIT:
By parsing the string of hours (i.e. "12:00") using SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a");, I get an error from JSON saying that the value is unparseable. If I use just SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm");, then there's no error but I can't get things to show up in 12-hour format.
If you look in the simple date format syntax docs you will find that 'h' is used for 12-hour time and 'a' is used for AM/PM. You will need to extract the two times using substring before putting them through the dateformatters.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat in = new SimpleDateFormat("<input format goes here>");
Date d = in.parse(INPUT_DATE_STRING);
SimpleDateFormat out = new SimpleDateFormat("<output format goes here>");
String outDate = out.format(d);
Try this:
//Char sequence for a 12 hour format.
CharSequence DEFAULT_FORMAT_12_HOUR = "hh:mm a";
//Char sequence for a 24 hour format.
CharSequence DEFAULT_FORMAT_24_HOUR = "kk:mm";
//date is the Date object. Look for more functions in format.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, date);
Let me know if it works. If you have any issue check the Date you are sending.
//This should give you the default time on the device. To show that it works.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, Calendar.getInstance());
I'm a newbie to android and struggling to achieve this.
I want to select date and time and also wants to add the selected date and time in one textfield with this format dd/mm/yyyy hh:mm.
I searched on this but couldn't find what I want to achieve.
any guidelines in terms of code or link would be appreciated.
Thanks
you can use SimpleDateFormat by which you can format the Date.
String format = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(yourdate));
use this you can your desired format
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
like this May 17, 2012 12:03:09 PM
If you need to make Custom Date and Time Picker then you can see how in this they have make.Android Date Slider
I have a Calendar object, and i know how to format it using android.text.format()
How can i get the phone settings for date and time to format the object to format set by the user. Like 24 hour clock , mmm dd yyyy...
What I wanted was to provide the date and time formatted according to the user's preferences and locale, in separate TextViews.
I have done this using static methods in the DateFormat class:
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); // Gets system date format
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(getApplicationContext()); // Gets system time format
txt1.setText(dateFormat.format(CalendarInstance.getTime())); // format() Returns a string according to the user's preferences (12-hr vs 24-hr) and locale
txt2.setText(timeFormat.format(CalendarInstance.getTime()));
Well, not exactly all the settings (such as 24 vs 12 hour format) selected by the user, but to use the configured locale you can use the DateUtils class.
I'm currently using the following code to format time on Google Android:
DateFormat.getDateTimeInstance().format(millis)
While this code honors my timezone and locale settings, it ignores the '24 hour mode' setting in system preferences, always returning time in AM/PM. Is there any way to get time in the same format as that on the status bar?
In theory (never tried this yet), you can use
http://developer.android.com/reference/android/text/format/DateFormat.html
To get formatters based on your settings. YMMV.
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
df.format(new Date(date));
"date" in millisec
Use the java.text.SimpleDateFormat class.