java.time
The legacy date-time API (java.util
date-time types and their formatting API, SimpleDateFormat
) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time
, the modern date-time API*.
Solution using modern date-time API:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String args[]) {
System.out.println(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH).format(LocalDate.of(2015, 6, 9)));
// Alternatively
System.out.println(LocalDate.of(2015, 6, 9).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
}
Output:
Tuesday
Tuesday
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.