How to get the current date and time of your timezone in Java?

Date is always UTC-based… or time-zone neutral, depending on how you want to view it. A Date only represents a point in time; it is independent of time zone, just a number of milliseconds since the Unix epoch. There’s no notion of a “local instance of Date.” Use Date in conjunction with Calendar and/or TimeZone.getDefault() … Read more

How to calculate elapsed time from now with Joda-Time?

To calculate the elapsed time with JodaTime, use Period. To format the elapsed time in the desired human representation, use PeriodFormatter which you can build by PeriodFormatterBuilder. Here’s a kickoff example: DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); PeriodFormatter formatter … Read more

Is there a class in java.time comparable to the Joda-Time Interval?

Sorry for you, there is no equivalent in JSR-310 to JodaTime-Interval-class. I have doubts if this will ever come, but project lead Stephen Colebourne considers at least to support it in the scope of his external library Threeten-Extra, see this issue. If you are happy with JodaTime you should keep it. Not everything in JodaTime … Read more

Using Joda Date & Time API to parse multiple formats

You can create multiple parsers and add them to the builder by using DateTimeFormatterBuilder.append method: DateTimeParser[] parsers = { DateTimeFormat.forPattern( “yyyy-MM-dd HH” ).getParser(), DateTimeFormat.forPattern( “yyyy-MM-dd” ).getParser() }; DateTimeFormatter formatter = new DateTimeFormatterBuilder().append( null, parsers ).toFormatter(); DateTime date1 = formatter.parseDateTime( “2010-01-01” ); DateTime date2 = formatter.parseDateTime( “2010-01-01 01” );

Difference in days between two dates in Java?

I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Days; Date past = new Date(110, 5, 20); // June 20th, 2010 Date today = new Date(110, 6, 24); // July 24th int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); … Read more

Add one day into Joda-Time DateTime

The plusDays method is not a mutator. It returns a copy of the given DateTime object with the change made rather than changing the given object. If you want to actually change the variable dateTime value, you’ll need: DateTime dateTime = new DateTime(date); dateTime = dateTime.plusDays(1);

Class broken error with Joda Time using Scala

Add this dependency: “org.joda” % “joda-convert” % “1.8.1” It’s an optional dependency of joda-time. I had to add it in my own project for the scala compiler to accept working with the joda-time jar. Your issue seems to be the same. Version is as at time of editing, latest versions can be found here

How to get the last date of a particular month with JodaTime?

How about: LocalDate endOfMonth = date.dayOfMonth().withMaximumValue(); dayOfMonth() returns a LocalDate.Property which represents the “day of month” field in a way which knows the originating LocalDate. As it happens, the withMaximumValue() method is even documented to recommend it for this particular task: This operation is useful for obtaining a LocalDate on the last day of the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)