-5

I'm trying to run a simple code that simulates a system for managing some cinema.

I have a Movie class, to which I want to add a Date.
(The Movie is part of a Cinema class that holds a Movies' array) The user, when inserting new data to the cinema, supposed to enter - besides the 'name' and 'rating' of the movie - a date and time. What would be the right way to set the date?

Should I create a new Calendar object to each and every Movie object and use it to set the date and time for every Movie?
Or should there be one Calendar object in Cinema that all Movie objects use to update the date and time (according to user input of course)?

so.very.tired
  • 2,958
  • 4
  • 41
  • 69

4 Answers4

2

If entering date trough console then it is most likely saved as a single String. You have to define a format in which the date is entered. Use that format with a SimpleDateFormat to create a Date object out of it.

Something like this should do it:

Scanner sc = new Scanner(System.in);
String dateFormat = "dd/MM/yyyy";
System.out.println("Please enter a date (" + dateFormat + "): ");
String dateString = sc.next();
Date date = new SimpleDateFormat(dateFormat).parse(dateString);
System.out.println(date.toString());
sc.close();
A4L
  • 17,353
  • 6
  • 49
  • 70
1

The answer by A4L is correct. But as Jon Skeet commented, you really should use Joda-Time. The java.util.Date/Calendar classes are notoriously bad, and are being supplanted in Java 8 by the java.time.* classes whose design is inspired by Joda-Time.

Be aware that unlike java.util.Date, a org.joda.time.DateTime knows its time zone.

Here's the Joda-Time version of A4L's example code.

You can choose or define other formats for parsing. I went with the standard ISO 8601 format.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

Scanner sc = new Scanner(System.in);
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
// A good practice is to always specify a time zone rather than depend on default.
parser = parser.withZone( DateTimeZone.forID( "America/New_York" ) );
System.out.println("Please enter a date (" + "yyyy-MM-dd'T'HH:mm:ss.SSSZZ" + "): ");
String dateString = sc.next();
DateTime dateTime = null;
try {
    dateTime = parser.parseDateTime(dateString);
    System.out.println( "dateTime: " + dateTime.toString() );
} catch ( IllegalArgumentException e ) {
    // e.printStackTrace();
    System.out.println( "The date you entered cannot be parsed." );
}
sc.close();

When run…

Please enter a date (yyyy-MM-dd'T'HH:mm:ss.SSSZZ): 
2013-12-13T23:45
dateTime: 2013-12-13T23:45:00.000-05:00
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Date is for holding a specific instant in time value. Calendar is for date / time manipulation such as getting day of weeek etc

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

If you use a single instance of Calendar then every movie will have the same date and time. I don't think this is probably what you want, so you should go with a different one for each movie.

Mark Wagoner
  • 1,729
  • 1
  • 13
  • 20