12

I'm using the PrettyTime java library for a variety of date/time processing in my java app, such as converting MySQL format dates/datetime strings into java dates, or the vice versa.

However, I see that date.getYear(), date.getMonth(), etc, are all deprecated, and it says to use Calendar instead. But PrettyTime only returns its results as Date objects, and I see no way to convert the Date objects into calendar objects.

In the documentation for Calendar, the only mention I see of Date is the method setTime(Date date), but the method name is ambigious, and the documentation is not clear on what calling this method would actually do. Obviously I can't just do calendar.set( date.getYear(), date.getMonth(), ..) etc, as those methods of Date are deprecated.

So how can I convert a given Date object to Calendar?

Ali
  • 261,656
  • 265
  • 575
  • 769

1 Answers1

10
Calendar cal = Calendar.getInstance();
cal.setTime(date);

You can get the calendar in different locales as well if you want.

You could also do

cal.setTimeInMillis(date.getTime());
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • 2
    Thanks, I wish the `setTime` method wasn't named so ambigiously or had a clearer documentation, so I won't have had to ask this. – Ali Oct 12 '13 at 02:19
  • As a general recommendation, if you can, try using Joda time. It's much cleaner than the Java standard time. – Jeff Storey Oct 12 '13 at 02:20
  • @ClickUpvote I would recommend using [joda time](http://www.joda.org/joda-time/) library instead or wait to Java 8 where the Date API is redesigned. – Luiggi Mendoza Oct 12 '13 at 02:20
  • @LuiggiMendoza Does it allow creating Date / Calendar objects from a given string, such as `YYYY-MM-DD HH:MM:SS`? That's my main requirement as I need to convert MySQL date/datetime strings into Java date/calendar, or vice versa. – Ali Oct 12 '13 at 02:21
  • @ClickUpvote yes, it does. – Luiggi Mendoza Oct 12 '13 at 02:22
  • @LuiggiMendoza Could you point me to the method for it? I don't see it at a quick glance through the documentation. – Ali Oct 12 '13 at 02:25
  • 1
    @ClickUpvote use a SEO. The first result of my search gave me this link: http://stackoverflow.com/q/6252678/1065197 – Luiggi Mendoza Oct 12 '13 at 02:28
  • @LuiggiMendoza It doesn't seem possible to covert a given `DateTime` to a string in a given format, though. – Ali Oct 12 '13 at 02:44
  • @ClickUpvote check here: http://stackoverflow.com/q/3709083/1065197. – Luiggi Mendoza Oct 12 '13 at 02:46
  • @LuiggiMendoza That's for a `LocalDateTime`, whereas the `DateTimeFormatter.parse()` returns just a `DateTime`.. – Ali Oct 12 '13 at 02:52
  • @ClickUpvote `DateTimeFormatter.print` is overloaded to support `ReadableInstant` (like `DateTime`) and `ReadablePartial` (like `LocalDate`). I would heavily recommend to start practicing and see the results for yourself instead of just searching *how can I foo the bar using Joda for an specific case that I won't test*. – Luiggi Mendoza Oct 12 '13 at 02:56