简体   繁体   中英

Java 8 Output date as YYYY-MM-DD

I have a getter in Java 8 that I set to the below, problem is the output is Sat Jan 11 00:00:00 AEDT 2020

The class has to remain as a Date , StartDate is a LocalDate .

public static Date getStartDate() {
    return Date.from(StartDate.atStartOfDay()
                      .atZone(ZoneId.systemDefault())
                      .toInstant());
}

I need it to return the value as YYYY-MM-DD and I am struggling to work it out.

Yes, this is an assignment. No idea on the logic where we've been told to set one to Date and the other as LocalDate , other than to annoy me......

Any help appreciated.

Please try this solution.

public static String getStartDate() {
    DateTimeFormatter oldPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
    DateTimeFormatter newPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDateTime datetime = LocalDateTime.parse(new Date().toInstant().toString(), oldPattern);
    return datetime.format(newPattern);
}

Straightforward Answer - SimpleDateFormatter

As you state that the type returned by getStartDate must remain a Date . In this case, the most straightforward way to do this is to use the SimpleDateFormat class:

You can pass the date into a SimpleDateFormatter :

SimpleDateFormat iso_8601_formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(iso_8601_formatter.format(getStartDate());

More detailed information can be found at this blog post.

Best Practice Answer - Use java.time

As noted by Ole VV, if it becomes possible to refactor the code, it would be best to unify the codebase around java.time instead of mixing that API with java.util.Date . This can be accomplished with a DateTimeFormatter

DateTimeFormatter iso_8601_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate start = StartDate.atStartOfDay();
System.out.println(start.format(iso_8601_formatter));

Note that DateTimeFormatter supplies the constant DateTimeFormatter.ISO_LOCAL_DATE for printing values in ISO-8601 format.

More information about the improvements made in the new API can be found in this article.

I think you can try this to get the date value as "yyyy-MM-dd"

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

        String date = simpleDateFormat.format(new Date());
        System.out.println(date);
    }
}

While returning an instance of java.util.Date , you cannot format it. It will always be returned as a Date object. In case you want it in string,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());

where new Date() is todays date, whatever date object you put, the sdf object ( java.text.SimpleDateFormat ) will do that. You can refer to java 8 docs of SimpleDateFormat for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM