-4

What is the proper way to make a date in android? "Date date = new Date, date = 1995/06/10"?

Also, how do i get a only the month of the date? most of the tutorials around have a simpledateformat with the "YYYY-MMD-dd HH mm ss", but how do i grab only the "MMM"?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    Use [Calender](https://developer.android.com/reference/java/util/Calendar.html) and [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). Read https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android – ADM Apr 22 '18 at 18:01
  • could you give an example please, i learn better that way – Jhungamesh Apr 22 '18 at 18:02
  • 2
    A lots of examples available in link above . And if you just try to search the same title you will get more posts like this. http://idownvotedbecau.se/noresearch/. – ADM Apr 22 '18 at 18:03
  • 3
    Possible duplicate of [How do you format date and time in Android?](https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – TapanHP Apr 22 '18 at 18:04
  • Don’t use the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Rather consider 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. Apr 23 '18 at 10:55

1 Answers1

1

Many programming languages are used for programming for Android. Since you mentioned the outdated SimpleDateFormat class, I am assuming that you are using Java, if not the language then at least the Java platform/runtime. Then I warmly recommend java.time, the modern Java date and time API. For example, using Java:

    LocalDate today = LocalDate.now(ZoneId.of("Asia/Manila"));
    LocalDate backThen = LocalDate.of(1995, Month.JUNE, 10);

Since it is never the same date everywhere on the planet, make sure you specify your desired time zone for getting today’s date. A LocalDate is a date without time of day.

To get only the month for use in your program:

    Month m = today.getMonth();

Running my code just now this returned Month.APRIL. Month is an enum with the 12 months of the Gregorian calendar.

If you wanted to print just the month to the user, your format pattern string should just contain only MMM (or MMMM for full month name):

    DateTimeFormatter monthFormatter 
            = DateTimeFormatter.ofPattern("MMM", Locale.forLanguageTag("fil-PH"));
    System.out.println(backThen.format(monthFormatter));

This printed:

Hun

I didn’t know June was Hunyo in Philippinese. I know now. Supply a different locale to get the month name in a different language.

And yes, very many tutorials and other pages still float around from when we used SimpleDateFormat and its now long outdated friends. Your best bet is to ignore them.

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, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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