0

actually I'm a .NET guy and I wonder why such a simple and trivial things as handling date/time objects is that complicated

Everything I need is a object which holds the parameters for Day, Month, Year, Hour, Minute, Second... and the possibility to manipulate theses values.

I don't want any conversion to any timezone. Values of parameter should have the value is set!

Question 1: How to create a date object from minutes? Question 2: How to create a date object from int's??

I've already spend hours to accomplish that and I can't believe that I really have to create my own DateTime implementation in Java!!

I tried it using Calendar(GMC, UTC...) and just Date but always the same. The values got from my Date-TimePicker are transformed to daylight saving time or some other rule corresponding to the calender I defined. One thing I really wonder about is that my time also get transformed when I use just "Date" object...

Cheers, Stefan

stefan
  • 1,336
  • 3
  • 21
  • 46
  • Please elaborate on your question 1, how can you expect to generate a valid Date by just entering minutes? – fvu Feb 02 '13 at 15:34
  • Actually you are right, therefore TimeSpan should be the object of choice... ... but what if you want to create a Date 23 Minutes AC ;-) – stefan Feb 02 '13 at 18:42
  • Am I right that by a "date object from minutes" you mean what they call an [Interval](http://joda-time.sourceforge.net/key_interval.html) in Joda? – fvu Feb 02 '13 at 18:53
  • FYI, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jul 04 '19 at 08:34

3 Answers3

2

I wonder why such a simple and trivial things as handling date/time objects is that complicated

The information technology industry as a whole went decades without tackling the surprisingly tricky problem of date-time handling.

Java originally inherited some code provided by Taligent/IBM. These became the java.util.Date,Calendar, GregorianCalendar, and related classes. They are terrible, bloody awful, flawed in design, and not object-oriented.

The Joda-Time project led by Stephen Colebourne was the first attempt I know of to tackle the date-time problem in a robust and competent manner. Later, Colebourne et al. took the lessons learned there to lead JSR 310, the java.time implementation, and related projects, the back-port in ThreeTen-Backport, and further additional functionality in ThreeTen-Extra.

So now Java has the industry-leading framework for date-time work in the java.time classes.

object which holds the parameters for Day, Month, Year, Hour, Minute, Second... and the possibility to manipulate theses values.

For a date-only, use LocalDate. For a date with time-of-day but lacking the context of a known offset-from-UTC or time zone, use LocalDateTime (not a moment). For a moment, a specific point on the timeline, use Instant (always in UTC), OffsetDateTime (a date with time-of-day in the context of a certain number of hours-minutes-seconds ahead/behind the prime meridian), or ZonedDateTime (a date and time with a time zone assigned).

Table of all date-time types in Java, both modern and legacy

Question 1: How to create a date object from minutes?

Not sure what you mean.

To represent a span-of-time unattached to the timeline, use Duration.

Duration d = Duration.ofMinutes( 10 ) ;
Instant now = Instant.now() ;
Instant tenMinutesFromNow = now.plus( d ) ;

If you have a count of minutes into the day, use LocalTime.MIN (00:00:00) with a Duration.

Duration d = Duration.ofMinutes( 10 ) ;
LocalTime lt = LocalTime.MIN.plus( d ) ) 

Question 2: How to create a date object from int's??

I am guessing you mean you want specify the year, month, day, and so on as a series of numbers. For that, use the LocalDateTime factory methad.

LocalDateTime ldt = LocalDateTime.of( 2020 , 1 , 23 , 15 , 30 , 0 , 0 ) ; // year, month, day, hour, minute, second, nanos fraction of second.

Or build up from a LocalDate and LocalTime.

LocalDate ld = LocalDate.of( 2020 , 1 , 23 ) ;
LocalTime lt = LocalTime.of( 15 , 30 ) ;
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ; 

As mentioned above, a LocalDateTime cannot represent a moment. To determine a moment, specify a time zone. For example, in our code above, do you mean 3:30 PM in Tokyo, Toulouse, or Toledo — thee different moments, several hours apart.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

Adjust into UTC by extracting a Instant.

Instant instant = zdt.toInstant() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Date and Calendar are not the best APIs in Java's library (to put it mildly, I consider them flaw-complementary), that's why a lot of Java programmers are using Joda-Time and many are looking forward to the inclusion in the standard distribution of JSR-310, a brandnew replacement for the old Date & Calendar mess. Joda has the added benefit that transitioning between Date and Joda's types is very easy (by using .toDate() or by using the relevant constructor that accepts a Date) so that using Joda's types inside your code and Date just at the "outer edges" where you interact with Date based code is not painful at all.

For your question 2, there's still a Date constructor that accepts int, and although it's deprecated you can still use it, see this thread. But I'd only suggest you take that route if switching to Joda isn't an option for you.

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
  • I can't believe that the standard implementation is that shit. I guess that using a DateTimePicker and storing the selected value somewhere, without any calendar transformation, is a really common task! – stefan Feb 02 '13 at 15:51
  • @stefan I believe every programming ecosystem has ugly sides, and Date/Calendar is widely recognized as one in Java - one of the bazillion webpages talking about it can be found [right here](http://stackoverflow.com/questions/87676/whats-the-best-way-to-manipulate-dates-and-timestamps-in-java). But despite their superficial obviousness, date and time calculations are actually pretty hard to get right, and designing an API that's complete, robust and intuitive is not an easy task. Although one may wonder if they actually need [5+ years](http://jcp.org/en/jsr/detail?id=310) to fix it – fvu Feb 02 '13 at 16:05
  • I agree that it isn't easy to create a complete date/time/calendar framework... But actually I was looking for something more primitive. In .NET you can use the Class "DateTime" which will never manipulate your data so that you can simple use it to store the collected date. This class further provides some verry usefull methods like AddMinutes(), AddHours()... which makes it verry easy to do calculations on that time object... – stefan Feb 02 '13 at 16:48
  • @stefan well in Java you can just use DateTime that has useful methods like plusMinutes, minusMinutes, etc. :-) My point being, Date and Calendar suck big time, and while we all impatiently wait for the final officially blessed solution (being JSR310, and one of the spec lead there is the author of Joda, Stephen Colebourne so I'm hopeful this time around they'll get it right) we just use Joda-Time. – fvu Feb 02 '13 at 18:50
0

I suggest that you take a look at Joda Time, it's java dates designed right.

  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 04 '19 at 08:34