Get Day of Week from Date in Java - Example Code
// // The following example code demonstrates how to // print out the Day of the Week from a Date object. // public class GetDayFromDate { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated System.out.println(simpleDateformat.format(now)); simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely System.out.println(simpleDateformat.format(now)); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // the day of the week in numerical format } }
Here is the output of the example code:
Wed Wednesday 4
2 comments:
This is wrong... "dd" gives you the day in the month. When you wrote this it worked b/c it was still the first week of the month.
@ Anonymous Thanks for pointing that out. I fixed it.
Post a Comment