It's not a secret anymore!
The moose likes Beginning Java and the fly likes Need to get a Day from Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need to get a Day from Date" Watch "Need to get a Day from Date" New topic

Need to get a Day from Date

kalaiyarasan sivaprakasam

Joined: Aug 17, 2009
Posts: 29
Hi all,

I have a query,

How to get a day from date,

For example there is a String called date="05/05/2010";
If it is Monday to Thurs Day it has to return Next Day and Date..otherwise it might be any one of Friday,Saturday,Sunday these it has to return Monday and its date,i meant next business day and its date.

is it possible in java Date and Calender class,

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

You have the date as a string, right? I would do it like this:

  • Parse the string into a Date object using a SimpleDateFormat object.
  • Get a Calendar object and call setTime() on it to set it to the date and time of the Date object.
  • Use the add() method of class Calendar to add days.
  • Use the get() method of class Calendar to get things like the day of week, etc.
  • To convert the date to a string again: call getTime() object on the Calendar to get a Date object, use a SimpleDateFormat to convert that to a string.

  • Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Sudipta Laha
    Ranch Hand

    Joined: Aug 23, 2010
    Posts: 49

    Try to use SimpleDateFormat to get the date object.

    Now you can call the getDay method of the date object to get the day of the week.

    After that logic can be implemented to get the required day of your choice using calender class or some other way(Storing days in array- was you have to do only two king of operation after comparing day with your array).

    Sudipta Kumar Laha
    Matthew Brown

    Joined: Apr 06, 2010
    Posts: 4543

    Sudipta Laha wrote:
    Now you can call the getDay method of the date object to get the day of the week.

    Date.getDay() is deprecated (and has been for some time) - it's much better to use the Calendar methods as Jesper describes.
    Tom Reilly

    Joined: Jun 01, 2010
    Posts: 618
    So you're saying all we have to do to create a four-day work week is write it in Java? Excellent! Now let's write the code to stop it from raining on weekends. I sure hope we won't need a deprecated method because I'll use it anyway.
    Rob Spoor

    Joined: Oct 27, 2005
    Posts: 20271

    If you're talking about the Monday to Thursday part, that's perfectly normal. For next business day support, the next business day for Friday will be Monday, not Saturday. I've written a lot of code with special cases for Friday and Saturday (Sunday usually behaves like the rest).

    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
    How To Ask Questions How To Answer Questions
    I agree. Here's the link:
    subject: Need to get a Day from Date
    It's not a secret anymore!