4

I am obtaining the current time from the mobile device and changing the format using the following code:

  SimpleDateFormat currentdate = new SimpleDateFormat("MMM,dd,yyyy HH:mm:ss s");
               date_n = currentdate.format(calendar.getTime());

However on some mobile devices I am getting the following output

Mar.,11,2021 12:08:26 26

A dot appears after Mar. I don't want that dot. How to handle it? Any guidelines?

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
Abbasi
  • 84
  • 7
  • What are "Some devices"? Do they all have the same system locale? – f1sh Mar 11 '21 at 09:24
  • use locale DateFormat dfEn = new SimpleDateFormat( "dd,MMM,yy", Locale.ENGLISH); – Majid Ali Mar 11 '21 at 09:28
  • means in my device it working ok out is ok but today someone please order and date store like this i don't know why this happening because my datetime format is this(ew SimpleDateFormat("MMM,dd,yyyy HH:mm:ss s");) – Abbasi Mar 11 '21 at 09:31
  • 1
    Do not longer use the outdated SimpleDateFormat and java.util.Date API. Use the modern java.time API – Jens Mar 11 '21 at 12:01
  • 1
    Never use a date-time formatter without a `Locale`. The same thing may be represented differently in different locales. Switch from the outdated `java.util.*` date-time API and `SimpleDateFormat` to the Java SE 8 API. 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](https://developer.android.com/studio/write/java8-support-table) and [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Arvind Kumar Avinash Mar 11 '21 at 15:08

2 Answers2

6

Try to specify a Locale (somewhere you want):

SimpleDateFormat currentdate = new SimpleDateFormat("MMM,dd,yyyy HH:mm:ss s", Locale.US);

Your problem is because how SimpleDateFormat displays a month depends on Locale. When you omit Locale, the default Locale of device's setting is used respectively. If the setting of a user's device sets a Locale which represents March as Mar., it does so. If you don't want that, you should specify a certain Locale explicitly.

hata
  • 11,633
  • 6
  • 46
  • 69
  • I want to know why this is happen because it works fine but today it generate this type of output and is this solution works ? – Abbasi Mar 11 '21 at 09:34
  • @MuhammadBasitJamilAbbasi I added an explanation. – hata Mar 11 '21 at 11:10
  • 1
    @MuhammadBasitJamilAbbasi a `Locale` does not just determine the language to be used but a specific style for abbreviations of names, too. That means a device with an English locale may abbreviate March by *Mar* or *Mar.*, you won't know. Now think about what might happen when someone uses your app on a device with a Spanish locale, for example. I can tell you the output of this code on my machine: It is `März,11,2021 12:18:41 41` – deHaar Mar 11 '21 at 11:18
  • ok thanks when I use Locale.English it works fine what ever the language is – Abbasi Mar 11 '21 at 11:55
1
SimpleDateFormat currentdate = new SimpleDateFormat("MMM,dd,yyyy HH:mm:ss s",Locale.ENGLISH);
hallywang
  • 146
  • 3