0

I want to get a java.util.Date class using joda. I need that the date in Date class will be format as yyyy-MMM-dd HH:mm:ss and GMT/UTC time.

Thanks.

I will clarify my question: i want that when i print toString of Date i will get the time in format of yyyy-MMM-dd HH:mm:ss and as UTC/GMT time.

Ggdw
  • 2,509
  • 5
  • 24
  • 22
  • I suggest you take a look at the [JavaDocs](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html). I can at least two ways this could be achieved... – MadProgrammer Jun 10 '13 at 07:00
  • `Date` objects don't **have** a format (or [time zone information](http://stackoverflow.com/questions/1516213/java-util-date-is-using-timezone), for that matter). What you want is probably a `Calendar` (which has time zone information, but still no format). – Joachim Sauer Jun 10 '13 at 07:12

2 Answers2

1
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MMM-dd HH:mm:ss");
DateTime jodaDate = fmt.parseDateTime(utilDate);
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • 1
    I think he is asking to get a `java.util.Date` from a jodaDate – vidit Jun 10 '13 at 07:03
  • I will clarify my question: i want that when i print toString of Date i will get the time in format of yyyy-MMM-dd HH:mm:ss and as UTC/GMT time. – Ggdw Jun 10 '13 at 07:33
0

There is method DateTime::toDate() inherited from AbstractInstant

You can format java.util.Date with
SimpleDateFormat

and you can format DateTime with
DateTimeFomratter

Or you can extend Date class

public class MyDate extends Date
{
   private SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

   public MyDate(Date date)
   {
      super(date.getTime());
   }

   @Override
   public String toString()
   {
      return FORMATTER.format(this);
   }
}
Ilya
  • 29,135
  • 19
  • 110
  • 158