- Change date string format in android
- 7 Answers 7
- java.time and ThreeTenABP
- Android Format date with time zone
- 10 Answers 10
- java.time
- Using the modern date-time API:
- Using the legacy API:
- Date formatting based on user locale on android
- 7 Answers 7
- java.time and ThreeTenABP
- Question: Can I use java.time on Android?
- Question: Can I also accept user input in the user’s local format?
- How do I change date time format in Android?
- 11 Answers 11
- How to format date and time in Android?
- 25 Answers 25
- SimpleDateFormat
- Date and Time format explanation
Change date string format in android
I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000
Now, I want this string as : Apr 18, 2012 01:25 PM
How can I do this?
7 Answers 7
UPDATE :-
As on, Date.parse(«Your date string»); is deprecated.
This will do it:
From oficial documentation, in the new API (18+) you should be implement this:
The easiest solution is to use DateFormat’s getter methods: The getDateTimeInstance DateFormat object formats date like that: Dec 31, 1969 4:00:00 PM, however there are those extra zeroes after 4:00
This is works for me, using SimpleDateFormat and Calendar
java.time and ThreeTenABP
The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat and/or DateFormat as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.
I suggest you separate your conversion into two operations. In you program don’t keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user’s time zone and format it into an appropriate string for that purpose.
Parse into an OffsetDateTime
The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.
Output from this snippet is:
Convert to user’s time zone and format
I ran this in Asia/Colombo time zone and got the desired:
Источник
Android Format date with time zone
I need to format the date into a specific string.
I used SimpleDateFormat class to format the date using the pattern » yyyy-MM-dd’T’HH:mm:ssZ » it returns current date as
» 2013-01-04T15:51:45+0530 » but I need as
» 2013-01-04T15:51:45+05:30 «.
Below is the coding used,
Output: formatted string: 2013-01-04T15:51:45+0530
I need the format as 2013-01-04T15:51:45+05:30 just adding the colon in between gmt time.
Because I’m working on Google calendar to insert an event, it accepts only the required format which I have mentioned.
10 Answers 10
You can also use «ZZZZZ» instead of «Z» in your pattern (according to documentation). Something like this
You can use Joda Time instead. Its DateTimeFormat has a ZZ format attribute which does what you want.
Big advantage: unlike SimpleDateFormat , DateTimeFormatter is thread safe. Usage:
You can use the pattern, yyyy-MM-dd’T’HH:mm:ssXXX for your desired output.
java.time
The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.
- For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
- If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Using the modern date-time API:
Building an object that holds date, time and timezone offset information and formatting the same:
Output:
As you can notice, the outputs for the default format and for that using a DateTimeFormatter are same. However, there is a catch here: the implementation of OffsetDateTime#toString omits the second and the fraction of second if they are 0 i.e. if the time in the above code is LocalTime.of(15, 0, 0) , the output for the default format will be 2013-01-04T15:00+05:30 . If you need a string like 2013-01-04T15:00:00+05:30 for this time, you will have to use a DateTimeFormatter with the desired pattern.
Parsing and formatting:
Output:
Using the legacy API:
Building a date-time object for the given timezone offset and formatting the same:
Output:
The Calendar class uses UTC (or GMT ) as its default timezone and therefore, unless you specify the timezone with it, it will return the java.util.Date object for UTC .
Similarly, the SimpleDateFormat class also uses UTC as its default timezone and therefore, unless you specify the timezone with it, it will return the formatted String for the corresponding date-time in UTC .
Источник
Date formatting based on user locale on android
I want to display a date of birth based on the user locale. In my application, one of my fields is the date of birth, which is currently in the format dd/mm/yyyy . So if the user changes his locale, the date format should also change accordingly. Any pointers or code samples would really help me to overcome the problem.
7 Answers 7
You can use the DateFormat class that formats a date according to the user locale.
You can use the different methods getLongDateFormat , getMediumDateFormat depending on the level of verbosity you would like to have.
While the accepted answer was correct when the question was asked, it has later become outdated. I am contributing the modern answer.
java.time and ThreeTenABP
It gives different output depending on the locale setting of the JVM (usually taking from the device). For example:
- Canadian French: Born: 91-10-13
- Chinese: Born: 1991/10/13
- German: Born: 13.10.91
- Italian: Born: 13/10/91
If you want a longer format, you may specify a different format style. Example outputs in US English locale:
- FormatStyle.SHORT : Born: 10/13/91
- FormatStyle.MEDIUM : Born: Oct 13, 1991
- FormatStyle.LONG : Born: October 13, 1991
- FormatStyle.FULL : Born: Thursday, October 13, 1991
Question: Can I use java.time on Android?
DateTimeFormatter.ofLocalizedDate requires Api O minimum.
Yes, java.time works nicely on older and newer Android devices. No, it does not require API level 26 or Oreo even though a message in your Android Studio might have you think that. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Question: Can I also accept user input in the user’s local format?
Yes, you can. The formatter can also be used for parsing a string from the user into a LocalDate :
I suggest that you first format an example date and show it to the user so that s/he can see which format your program expects for his/her locale. As example date take a date with day of month greater than 12 and year greater than 31 so that the order of day, month and year can be seen from the example (for longer formats the year doesn’t matter since it will be four digits).
Parsing will throw a DateTimeParseException if the user entered the date in an incorrect format or a non-valid date. Catch it and allow the user to try again.
Источник
How do I change date time format in Android?
I am displaying the date and time in Android with this format:
2013-06-18 12:41:24
How can I change it to the following format?
18-jun-2013 12:41 pm
11 Answers 11
Here is working code
Use this code like below:
Hope it will help you.
I modified Rusabh’s answer for general purpose formatting of date in my project
Usage:
You have to set like this
set answer like this to set date format
use this code below:
First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it.
You can get data using get() method in Calendar class.
We can use this format for convert the date. Pass the date as parameter
This is the best choice available on Android which will format time based on device default Locate
Example output depending on Locale:
- USA — 02/18/2021, 1:00:00 PM (USA uses 12 format time)
- Ukraine — 18.2.2012, 13:00:00 (Ukraine uses 24 format time)
So it does all the hard job automatically for you
P.S. replace yy with yyyy if you need 2021 instead of 21, MM to MMM if you want display month as word instead of number and so on
For example «dMMMyyyyjjmmss» for USA will return:
Источник
How to format date and time in Android?
How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?
25 Answers 25
Use the standard Java DateFormat class.
For example to display the current date and time do the following:
You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.
In my opinion, android.text.format.DateFormat.getDateFormat(context) makes me confused because this method returns java.text.DateFormat rather than android.text.format.DateFormat — -«.
So, I use the fragment code as below to get the current date/time in my format.
In addition, you can use others formats. Follow DateFormat.
You can use DateFormat . Result depends on default Locale of the phone, but you can specify Locale too :
This is results on a
FR Locale : 3 nov. 2017
US/En Locale : Jan 12, 1952
FR Locale : 03/11/2017
US/En Locale : 12.13.52
FR Locale : 3 nov. 2017
US/En Locale : Jan 12, 1952
FR Locale : 3 novembre 2017
US/En Locale : January 12, 1952
FR Locale : vendredi 3 novembre 2017
US/En Locale : Tuesday, April 12, 1952
FR Locale : 3 nov. 2017 16:04:58
FR Locale : 03/11/2017 16:04
FR Locale : 03/11/2017 16:04:58
FR Locale : 03/11/2017 16:04:58 GMT+01:00
FR Locale : 03/11/2017 16:04:58 heure normale d’Europe centrale
FR Locale : 16:04:58
FR Locale : 16:04
FR Locale : 16:04:58
FR Locale : 16:04:58 GMT+01:00
FR Locale : 16:04:58 heure normale d’Europe centrale
Date to Locale date string:
-> Dec 31, 1969 4:00:00 PM
This will do it:
Use SimpleDateFormat
Is better to use Android native Time class:
Use these two as a class variables:
And use it like this:
Here is the simplest way:
This is my method, you can define and input and output format.
SimpleDateFormat
I use SimpleDateFormat without custom pattern to get actual date and time from the system in the device’s preselected format:
returns:
Date and Time format explanation
Use build in Time class!
This code work for me!
%tR is short for %tH:%tM , means to reuse last parameter( 1$ ).
It is equivalent to String.format(«%1$tY-%1$tm-%1$td %1$tH:%1$tM», Calendar.getInstance())
Date format class work with cheat code to make date. Like
- M -> 7, MM -> 07, MMM -> Jul , MMMM -> July
- EEE -> Tue , EEEE -> Tuesday
- z -> EST , zzz -> EST , zzzz -> Eastern Standard Time
You can check more cheats here.
The other answers are generally correct. I should like to contribute the modern answer. The classes Date , DateFormat and SimpleDateFormat used in most of the other answers, are long outdated and have caused trouble for many programmers over many years. Today we have so much better in java.time , AKA JSR-310, the modern Java date & time API. Can you use this on Android yet? Most certainly! The modern classes have been backported to Android in the ThreeTenABP project. See this question: How to use ThreeTenABP in Android Project for all the details.
This snippet should get you started:
When I set my computer’s preferred language to US English or UK English, this prints:
When instead I set it to Danish, I get:
So it does follow the configuration. I am unsure exactly to what detail it follows your device’s date and time settings, though, and this may vary from phone to phone.
Источник