-8

I actually use this

Calendar calendar = Calendar.getInstance();
final String currentDate = 
DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

result = wednesday 27 march 2019

I need this : 27032019 Without , / or . Only XXXXXXXX

Thanks you

Braindead
  • 1
  • 2
  • 1
    Research SimpleDateFormat. But I think this should work: SimpleDateFormat format = new SimpleDateFormat("ddMMyyy") – Janwilx72 Mar 27 '19 at 10:31
  • 1
    try using new java 8 time api's LocalDate. Date is now outdated. – Jabongg Mar 27 '19 at 10:33
  • As an aside consider throwing away the long outmoded and notoriously troublesome `DateFormat` 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. `LocalDate.now(yourTimeZone).format(DateTimeFormatter.ofPattern("ddMMuuuu"))`. – Ole V.V. Mar 27 '19 at 12:14
  • What do you need `27032019` for? It’s not easily readable by humans and not recommended for serialization (for the latter rather use [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format: `2019-03-27`). – Ole V.V. Mar 27 '19 at 12:56

4 Answers4

2

Here’s the modern answer and some additional thoughts.

For most purposes you should not want your date formatted to 27032019. It’s not easily readable by humans and not recommended for serialization.

Also consider not using Calendar, DateFormat, SimpleDateFormat or Date. Those classes are long outdated and poorly designed. Instead you may use java.time, the modern Java date and time API. It’s so much nicer to work with.

Serialization using java.time and ThreeTenABP

If you need your date string to be machine readable — for example if it’s for a JSON or for storing into a text file from where you or someone else needs to read it back — use the standard ISO 8601 format. LocalDate.toString produces this format, so we don’t need any explicit formatter:

    final String currentDate
            = LocalDate.now(ZoneId.of("Africa/Khartoum")).toString();
    System.out.println(currentDate);

Output when running today is:

2019-03-27

A compacter ISO 8601 format

If you insist on a compact format without any punctuation, ISO 8601 offers that too:

    final String currentDate = LocalDate.now(ZoneId.of("Africa/Khartoum"))
            .format(DateTimeFormatter.BASIC_ISO_DATE);

20190327

Your format

And if you really insist (I don’t see why you should), java.time can of course produce your requested format too:

    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("ddMMuuuu");
    final String currentDate = LocalDate.now(ZoneId.of("Africa/Khartoum"))
            .format(dateFormatter);

27032019

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. My imports for the above snippets are:

    import org.threeten.bp.LocalDate;
    import org.threeten.bp.ZoneId;
    import org.threeten.bp.format.DateTimeFormatter;
    

Links

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

Use SimpleDateFormat:

Calendar date = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy");
System.out.println("Date : " + format.format(date.getTime()));;
S-Sh
  • 3,564
  • 3
  • 15
  • 19
0

Well what you can do is that using short format and taking out the unnecessary parts

final String currentDate = 
DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());
currentDate= currentDate.replace("/"", "");
naïveRSA
  • 113
  • 7
  • In en-US locale this gives 32719. There are probably locales where it works, but result varies by locale. – Ole V.V. Mar 27 '19 at 18:04
-1

try this:

    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
    System.out.println(dateFormat.format(date));
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Jabongg
  • 2,099
  • 2
  • 15
  • 32