01 August, 2012

Find day name from an Input Date in Java

There are many ways to find the day name from a given date. But here I have used simple one.Really its so easy but sometimes we hang on it .We never found the solutions.Today its happening with me, because I always prefer  less search on Google.

Below the code for finding the day name from an input date.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class myDay{
public static void main(){
                String inputDate="01/08/2012";
                SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
                Date dt1=format1.parse(dt);
                DateFormat format2=new SimpleDateFormat("EEEE");
                String finalDay=format2.format(dt1);
                System.out.println("My Day is: "+finalDay);
}
}


Output:- Wednesday

Explanation :- 

First I prepare a date by using SimpleDateFormat . Then Prepare a DateFormat by using that before SimpleDateFormat(format1).

EEEE is the date Format Suffix for Day.More

No comments:

Post a Comment