3
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?

hata
  • 11,633
  • 6
  • 46
  • 69
Shon22
  • 57
  • 2
  • 11
  • You can get date and change its structure manually – MMG Apr 24 '20 at 13:56
  • 1
    Which Android version are you using? – MC Emperor Apr 24 '20 at 14:09
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `DateFormat` class and friends, 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. Apr 24 '20 at 14:48

4 Answers4

5

You can use SimpleDateFormat class:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String currentDate = sdf.format(calendar.getTime());
hata
  • 11,633
  • 6
  • 46
  • 69
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.

Fred
  • 137
  • 1
  • 7
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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161