INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

java.util.calendar

java.util.calendar

(OP)
thread269-1703513: Working with dates
What is the best way to store a simple date.
Should I just store it as a String or should I go through the process of using java.util.calendar or something similar?

I found this code on the internet and is a good example to work with dates

CODE -->

import java.util.Calendar;
import java.util.Date;

public class GetTheCurrentTime {
  public static void main( String[] args ) {
    // one way
    long currentTimeInMillis = System.currentTimeMillis();
    Date today = new Date( currentTimeInMillis );
    System.out.println( today );

    // another way
    Calendar cal = Calendar.getInstance();
    today = cal.getTime();
    System.out.println( today );
  }
} 

RE: java.util.calendar

I will depend on what are you planning to do with the date: if you want to add days, check if it's in a range or store it in a DB with DateTime type, then it's the way to go.

Cheers,
Dian

RE: java.util.calendar

Hi

Quote (Dian)

(...) the it's the way to go.
Missing anything ⇑ here, between "the" and "it's" ? ( My rusty Java says Calendar should go there. )

Feherke.
feherke.ga

RE: java.util.calendar

2
Actually it was just a missing "n" (post edited).

I was trying to clarify the choice between a String or a Java class. A String is what it is: a chunk of characters, so your program won't distinguish between "01/01/16" and "56/00/98" but if your requirements don't involve validation or manipulation, well, String could be an approach.

In any other cases, objects are the way to do. For a long time pipe, working with date and time in Java was a nightmare and you had to choose between Date and Calendar with no clear recommendation and an endless debate

With Java 8, the new java.time arrived and brought a unified way to deal with date and time. The brand new LocalDate, LocalTime and LocalDateTime end the the debate and (hopefully) will make everyone work the same way, as shown here.

Cheers,
Dian

RE: java.util.calendar

Hi

I see. Indeed, those new classes seems to be able to do everything a programmer may expect from them. ( Wondering how nice could they play together with java.sql.Date, but that is another story. )

Thank you, for the information.

Feherke.
feherke.ga

RE: java.util.calendar

I think this time pipe they nailed it and directly added new methods to java.sql.Date:

SQL Date -> Local Date: toLocalDate
Local Date -> SQL Date: valueOf

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close