24

And how to print it as a string.

I tried this but I get the date in (YYYMMMMDDD HHSSMM) format:

System.out.println(LocalDateTime.now());

What is the easiest way to get the current date in Basic ISO 8601 (YYYYMMDD) format?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
ron cohen
  • 295
  • 1
  • 3
  • 9
  • http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Dragondraikk Jun 30 '15 at 12:40
  • possible duplicate of [print current date in java](http://stackoverflow.com/questions/26717733/print-current-date-in-java) – hagrawal7777 Jun 30 '15 at 12:40
  • possible duplicate of [How to get the current date/time in java](http://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java) – Fjodr Jun 30 '15 at 12:41
  • 1
    possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – RealSkeptic Jun 30 '15 at 12:54

6 Answers6

36

is that what you are looking for?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
System.out.println(LocalDate.now().format(formatter));
user902383
  • 8,420
  • 8
  • 43
  • 63
  • More details here http://www.journaldev.com/2800/java-8-date-time-api-example-tutorial-localdate-instant-localdatetime-parse-and-format – stolen_leaves Jun 30 '15 at 13:00
  • 7
    This, or just [`DateTimeFormatter.BASIC_ISO_DATE`](http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#BASIC_ISO_DATE) instead of the formatter creation. – Petr Janeček Jun 30 '15 at 13:09
  • 2
    Uppercase `YYYY` is wrong, you need lowercase `yyyy` for year of era or `uuuu` for (signed) year. – Ole V.V. Jun 04 '19 at 19:29
23

This does the trick but may not be the easiest:

import java.util.*;
import java.text.*;

class Test {
    public static void main (String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        System.out.println(dateFormat.format(date));
    }
}
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
  • 6
    I see you commenting that under every answer here but I don't see your answer yet. Mind to share? – JackWhiteIII Jun 30 '15 at 12:52
  • 3
    When `LocalDateTime` from [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is asked about, please don’t introduce the long outdated and notoriously troublesome `SimpleDateFormat` class. Today we have so much better in java.time and its `DateTimeFormatter`. – Ole V.V. Jun 04 '19 at 19:28
  • If you also prefer to specify the Java class files to import, we can use the following: import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; – masarapmabuhay Sep 16 '20 at 07:17
  • Best answer for API < 26 – luismiguelss Jul 08 '21 at 08:35
10
DateTimeFormatter.BASIC_ISO_DATE.format(LocalDate.now());
tmx
  • 446
  • 6
  • 7
8

This is a very old question but gets me every time. There is a much simpler way now in one line:

String now = Instant.now().atZone(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(now);

outputs: 2020-07-09

markthegrea
  • 3,731
  • 7
  • 55
  • 78
4

Just use: SimpleDateFormat

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();        
// Using DateFormat format method we can create a string 
// representation of a date with the defined format.
String reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);

http://www.mkyong.com/java/how-to-convert-string-to-date-java/

Hakim
  • 434
  • 7
  • 21
  • 9
    In 2015, I would like to see the default answer using the `java.time` package, and NOT the obsolete `Date` class and friends. – Petr Janeček Jun 30 '15 at 12:50
0

tl;dr

Use the built-in formatter.

LocalDate.now().format( DateTimeFormatter.BASIC_ISO_DATE ) 

20240123

LocalDate

Understand that determining today's date requires a time zone. For any given moment, the date varies around the globe by time zone.

Capture the current date using the JVM’s current default time zone.

LocalDate today = LocalDate.now() ;

Better to specify the desired/expected time zone.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
LocalDate today = LocalDate.now( z ) ;

For the “Basic” version of ISO 8601 format (YYYYMMDD), use the predefined constant formatter object.

String output = today.format( DateTimeFormatter.BASIC_ISO_DATE ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154