• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Need to get a Day from Date

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,


thanks,
Kalai.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

  •  
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    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).
     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Rancher
    Posts: 618
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Sheriff
    Posts: 22801
    131
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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).
     
    rubbery bacon. crispy tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic