0

How to change date format from 14-feb-2019 to 14-02-2019 in android studio, actually I am picking my system date but I want to change Feb to month number in Android Studio, here is my code snippets:

eddate = (EditText) findViewById(R.id.editdate);
edtime = (EditText) findViewById(R.id.editime);
eddate.setFocusable(false);
Calendar calendar = Calendar.getInstance();
String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
String[] arr=currentDate.split(" ");
String date=arr[0]+"-"+arr[1]+"-"+arr[2];
// Toast.makeText(this, ""+date, Toast.LENGTH_SHORT).show();
eddate.setText(date);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Try this : `new SimpleDateFormat("dd-MMM-yyyy").format(c.getTime())` – Pratik Butani Feb 14 '19 at 12:49
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `DateFormat` and friends like `Calendar`, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Feb 14 '19 at 19:23
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 14 '19 at 21:18

3 Answers3

1

Try this will help you

public class Test {

    public static void main(String[] args) {
        String parseddate = parseDateToddMMyyyy("14-feb-2019");
        System.out.println(parseddate);
    }


    public static String parseDateToddMMyyyy(String time) {
        String outputPattern = "dd-MM-yyyy";
        String inputPattern= "dd-MMM-yyyy";
        SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

        Date date = null;
        String str = null;

        try {
            date = inputFormat.parse(time);
            str = outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

}
Dharmeet Soni
  • 204
  • 1
  • 7
  • 1
    @OleV.V.this code throws an exception if you take date from the first message "14-feb-2019", also not even necessary Date date = null; put out of try block –  Feb 15 '19 at 13:24
  • @Eugene see my another ans I put running code for you – Dharmeet Soni Feb 15 '19 at 14:05
  • @Eugene i tryied and run that code thats why I put the snippet if you cannot run then need to learn boy u r little for that , if you dont know then dont downword the code – Dharmeet Soni Feb 15 '19 at 14:13
  • "need to learn boy" said unknown guy who event can't remove warning from his code –  Feb 15 '19 at 14:22
  • for people who will read this discussion a would like leave a comment - that dirty code above is lacking Locale parameter for SimpleDateFormat –  Feb 15 '19 at 14:49
1

You are using the built-in date format for your locale, which is a good idea. It’s simple, you are exploiting that someone knows what that format looks like, and your code lends itself well to internationalization. When you do that, you can choose how long or short of a format you want. You probably did something equivalent to the following:

    ZoneId zone = ZoneId.of("Asia/Karachi");
    Locale pakistan = Locale.forLanguageTag("en-PK");
    DateTimeFormatter mediumFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM).withLocale(pakistan);

    LocalDate today = LocalDate.now(zone);
    System.out.println(today.format(mediumFormatter));

15-Feb-2019

In my snippet I have specified a medium format. I think that your best bet is to use the short format instead:

    DateTimeFormatter shortFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.SHORT).withLocale(pakistan);
    System.out.println(today.format(shortFormatter));

15/02/2019

This uses slashes instead of hyphens. I would trust that this is how people in your culture generally will expect to see a date when written in a short format. And you’re saved of your string manipulation or other hand formatting.

In my snippets I am using java.time, the modern Java date and time API. Calendar and DateFormat are long outdated, the latter in particular notoriously troublesome. The modern API is so much nicer to work with.

Disclaimer: I have run the snippets on my Java 10. Output on Android may vary. I wouldn’t be too worried. In all cases the built-in localized formats have been chosen with care.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. 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 use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
String dateFormat= "dd-MM-yyyy";
Date date = calendar.getTime();
String dateText= new SimpleDateFormat(dateFormat).format(date);
Ivan
  • 557
  • 1
  • 3
  • 14