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?
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?
is that what you are looking for?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
System.out.println(LocalDate.now().format(formatter));
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));
}
}
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
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/
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 ) ;