Android - change Date Format to dd/mm/yy
Asked Answered
G

4

6
Calendar calendar = Calendar.getInstance();
String currentDate = DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());

I'm trying to get the current date but in a format of DD/MM/YY, but it gives me MM/DD/YY Any suggestions about how to fix it?

Gabelle answered 24/4, 2020 at 13:52 Comment(3)
You can get date and change its structure manuallyAcrobat
Which Android version are you using?Chandelier
As an aside consider throwing away the long outmoded and notoriously troublesome DateFormat class and friends, and adding 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.Woodhead
B
9

You can use SimpleDateFormat class:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String currentDate = sdf.format(calendar.getTime());
Bastien answered 24/4, 2020 at 14:0 Comment(0)
V
1

I assume you would like to reverse the date format.

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(dateString);

SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy");
return fmtOut.format(date);

Hoping it will be helped.

Verecund answered 24/4, 2020 at 14:8 Comment(0)
P
1

tl;dr

The modern approach uses java.time classes.

LocalDate
.now
(
    ZoneId.of( "Africa/Casablanca" )
)
.format
(
    DateTimeFormatter.ofPattern( "dd/MM/uu" )
)

Avoid legacy date-time classes

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

java.time

For a date only value, without a time-of-day and without a time zone, use LocalDate.

A time zone is required to determine the current date. For any given moment, the date may by “tomorrow” in Japan while still “yesterday” in Mexico.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate ld = LocalDate.now( z ) ;

Generate a string with text in standard ISO 8601 format.

String output = ld.toString() ;

Generate a string with text in your custom format.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uu" ) ;
String output = ld.format( f ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Pitchblende answered 24/4, 2020 at 15:14 Comment(0)
W
0

I'm trying to get the current date but in a format of DD/MM/YY, …

Don’t. Trust that the user knows his or her locale best, and the locale data built into Java know the proper date format for that locale best. While 24/04/20 may be a fine format for your locale, on devices in other locales a different format is likely to be preferred.

Using java.time, the modern Java date and time API:

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    LocalDate d = LocalDate.now(ZoneId.systemDefault());
    System.out.println(d.format(dateFormatter));

BTW in many locales including Yiddish, Gallegan, Indonesian, Uzbek, Tajik, Somali, Malay, Italian and New Zealand English the output will be what you said you wanted:

24/04/20

I do recommend that you use java.time for your date work. DateFormat is a notoriously troublesome class, and Calendar is poorly designed too. Both are long outdated. And java.time is so much nicer to work with.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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

Woodhead answered 24/4, 2020 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.