-   Programming (
-   -   Getting Day of Week - Java - SQL Date (

MRMadhav 04-03-2006 01:38 AM

Getting Day of Week - Java - SQL Date
I am currently using java.sql.Date as my default date for compatibility with mysql database. I want to get the day in string of any particular date. E.g 2006-04-03 should return "Monday". I am using this for some validations where the user inputs the day name i.e Mondey,Tuesday... and afterwards when they enter a certain date and my application must check if that day is correct.

Mega Man X 04-03-2006 01:56 AM

In Java, the day of the week are Integers. 1 is Sunday, 2 is Monday, ..., and 7 is Saturday. I'm not sure if this will be the same with sql.Date, but you can try it :)


import java.util.*;

public class MainClass {

    public static void main(String[] args) {
        Calendar calender = new GregorianCalendar(2006, Calendar.APRIL, 3);
        int dayOfWeek = calender.get(Calendar.DAY_OF_WEEK);   
        System.out.println(dayOfWeek); // This will return 2. 2=Monday



MRMadhav 04-03-2006 07:07 AM

Thanks Mega Man X! Its so stupid of me not to think about the Calander methods in java.util. I've already used it some time back with java.sql.Date and it surely works! Thanks Again...

Mega Man X 04-03-2006 08:17 AM

You're welcome MRMadhav!. I'm glad it works. By the way, thanks for replying. I soon will try to build a JSP page with SQL and now I know this will work later for me as well :)

MRMadhav 04-03-2006 01:01 PM

Sure Am glad that this will help you too, but remember one thing when you use:
import java.sql.*;

and you are using the Date function, don't put

import java.util.*;

as both have the same method for date but each respond differently!

All times are GMT -5. The time now is 10:27 AM.