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 (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));
}
}
DateTimeFormatter.BASIC_ISO_DATE.format(LocalDate.now());
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With