Date in constructor in Java
Asked Answered
E

4

5

I have a class with the constructor:

...
Date d1;
Date d2;
public DateClass(Date d1, Date d2) {
   this.d1 = d1;
   this.d2 = d2;
}
...

Then in another class I want to use this constructor:

...
DateClass d = new DateClass(what I should I write here to create Date in format mm-dd-yyyy);
System.out.println(d);
...

Thank you! P.S This in not a part of a homework. I really do not know how to do it.

Eke answered 10/12, 2011 at 20:51 Comment(1)
For new users to this question, use java.time, the modern Java date and time API, for your date and time work. The Date class had severe design problems and has been outdated the last 10 years. For a date use the LocalDate class and instantiate it with for example LocalDate.of(2011, Month.DECEMBER, 10).Calamus
K
3

You can make a new Date by calling the constructor.

// you specify year1, month1, day1
DateClass d = new DateClass(new Date(year1-1900, month1-1, day1),
    new Date (year2-1900, month2-1, day2);

This is the simplest way, and will certainly work; however, as Carlos Heuberger correctly notes, this is deprecated, meaning that other ways are preferred. You can also make a new Date through DateFormat:

DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
Date d1 = df.parse("12-10-2011"); // for example, today's date
Date d2 = df.parse("01-01-1900"); // use your own dates, of course

To be able to print in mm-dd-yyyy format, implement toString:

public String toString(){
    DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
    return "Date 1: " + df.format(d1) + " Date 2: " + df.format(d2);
}
Kagu answered 10/12, 2011 at 21:2 Comment(2)
1 - the constructor is deprecated. 2 - should use month-1. 3 - should be year-1900 (2011 or just 11 would result in wrong years). All together not the best choice... (e.g new Date(2011, 12, 10) will be January 10th 3912; today is new Date(111, 11, 10))Hortense
@CarlosHeuberger Thanks for the suggestions! You are right on all points.Kagu
M
7

Date doesn't have a format. It's just an instant in time, with no associated calendar or time zone. When you need to format a Date, you would often use a DateFormat which is told the calendar system to use, the time zone to convert the instant into a local time etc.

When you print out a Date as you're doing in the second snippet, implicitly via toString(), that will always use the system default time zone, and an unmodifiable format. (It may or may not change with system locale - I'm not sure). Basically that should only be used for debugging. If you want any sort of control over the text, DateFormat is where it's at.

If you want to be able to simply construct date values from year/month/day etc, I'd recommend you look at Joda Time - it's a much saner date/time API than Java's. It makes all kinds of things much cleaner, including the separation of "local time", "local date", "local date and time", "date and time in a particular time zone" etc.

Munt answered 10/12, 2011 at 20:53 Comment(2)
I think he meant to parse a date with that format, not to create a date with that format.Equidistant
@Tudor: It's basically unclear - but either way I'd suggest using Joda Time :)Munt
E
3

You need to use a SimpleDateFormat (assuming you have the two dates as strings):

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Date d1 = format.parse(d1String);
Date d2 = format.parse(d2String);
DateClass d = new DateClass(d1, d2);
Equidistant answered 10/12, 2011 at 20:55 Comment(3)
I second this. Keep in mind that [SimpleDateFormat][1] is not thread safe. [1]: docs.oracle.com/javase/7/docs/api/java/text/…Zola
what id d1String? you mean if I have 08-08-94 as d1String then it works?Eke
@Eke but beware, that would be year 0094, not 1994, since using yyyy (instead of yy) for the yearHortense
K
3

You can make a new Date by calling the constructor.

// you specify year1, month1, day1
DateClass d = new DateClass(new Date(year1-1900, month1-1, day1),
    new Date (year2-1900, month2-1, day2);

This is the simplest way, and will certainly work; however, as Carlos Heuberger correctly notes, this is deprecated, meaning that other ways are preferred. You can also make a new Date through DateFormat:

DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
Date d1 = df.parse("12-10-2011"); // for example, today's date
Date d2 = df.parse("01-01-1900"); // use your own dates, of course

To be able to print in mm-dd-yyyy format, implement toString:

public String toString(){
    DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
    return "Date 1: " + df.format(d1) + " Date 2: " + df.format(d2);
}
Kagu answered 10/12, 2011 at 21:2 Comment(2)
1 - the constructor is deprecated. 2 - should use month-1. 3 - should be year-1900 (2011 or just 11 would result in wrong years). All together not the best choice... (e.g new Date(2011, 12, 10) will be January 10th 3912; today is new Date(111, 11, 10))Hortense
@CarlosHeuberger Thanks for the suggestions! You are right on all points.Kagu
S
1

The Answers by Allen Z. and by Jon Skeet are both correct.

java.time

Here is an update regarding the modern java.time classes in Java 8+ that supplanted the terribly flawed legacy classes such as Date.

org.threeten.extra.Interval & java.time.Instant

Firstly, your DateClass that represents a span of time need not be invented by you. You can find an already written class in the ThreeTen-Extra library that extends the java.time classes built into Java. There you will find the org.threeten.extra.Interval class, that represents a pair of java.time.Instant class.

To use that class, pass a pair of Instant objects.

Notice that in java.time we do not instantiate by calling new. Instead we use static factory methods such as .of.

Instant now = Instant.now() ;
Instant later = now.plusHours( 1 ) ;
Interval interval = Interval.of( now , later ) ;

From there you call useful methods such as contains, overlaps, abuts, and so on.

org.threeten.extra.LocalDateRange & java.time.LocalDate

If you want a span of time covering dates only, pass a pair of java.time.LocalDate objects to get a org.threeten.extra.LocalDateRange object.

LocalDate today = LocalDate.now() ;  // Better to pass the optional time zone.
LocalDate weekLater = today.plusWeeks( 1 ) ;
LocalDateRange dateRange = LocalDateRange.of( today , weekLater ) ;

Again, you can call handy methods like contains, overlaps, abuts, and more.

Serif answered 9/9, 2024 at 4:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.