Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with dates  RSS feed

 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys
its been such a long time since i last visited this forum!
im currently enjoying my 4( ) months holiday from college (i dont wana do back ), but ive decided to ruin my holiday and do some java !! i've luckily finished the java modules i needed to get my degree , but i still want to have more knowledge than i gained in the past 2 years! unfortunetely i didnt work with applets and thats what im here for...

my problem at the moments is dealing with dates! ive never learnt anything about it! The program i want to write is for a newsagent, each item stored in the system will require an ExpireyDate field... thats what ive done so far


am i so far in the right path ? and what difference would it make if i replace "GregorianCalendar" with "Date" or "Calender"?

cheers ppl
Hannah
[ July 07, 2005: Message edited by: H Melua ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks OK so far. A few suggestions:
  • I'd use Date objects, rather than Calendar in your method signature. An expiry date is a specific point in a Calendar after all.
  • Swapping the implementation GregorianCalendar with the interface Calendar is good practice. In this case the benefits may seem less than obvious (since using a Calendar other than the GregorianCalendar may seem very unlikely), but typically in any language if you code to an interface you leave room for future changes.
  • Look at the java.text.DateFormat class for a way to convert Strings to dates.

  •  
    Joel McNary
    Bartender
    Posts: 1840
    Eclipse IDE Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dates in Java are expressed as milliseconds since the epoch. This, of course, conflicts with our concept of dates as "July 7, 2005" or "1 Tamuz, 5765" or "30 Jumada I, 1426"

    To get the current time, simple create a new java.util.Date()
    To convert a Date to and from a String, use the java.text.SimpleDateFormat class.
    To manipulate Dates, use a Calendar. I would not use GregorianCalendar per se, but rather Calendar.getInstance(). Set the Calendar to a certain date with teh setTime() method, perform the manipulations (usually using the set(int, int) or the add(int, int) methods and the Calendar constants), and the retreive the date with the getTime() method.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!