How to convert date in to yyyy-MM-dd Format in Java


In Java, you can convert a date into a yyyy-MM-dd format using the SimpleDateFormat class. Here’s an example of how you can do this:

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

public class DateConverter {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);
        System.out.println(formattedDate);
    }
}

In this example, we first create a Date object to represent the current date and time. Then, we create a SimpleDateFormat object that specifies the format we want to use for the date (yyyy-MM-dd). Finally, we use the format() method of the formatter object to convert the date into a string in the specified format. The output will be a string in the format “yyyy-MM-dd”, representing the current date.