1

As we know we can get date using Java.util.Date or Calender instance or by using GregorianCalendar instance .

Using class 'SimpleDateFormat' we can parse() or format() function our Date or String.

As we know using parse() or format() function we get output as a String only.

Is there any option available in java to get Date instance in particular format like 'yyyy-MM-dd' or any other format, but final output should be of Date type with this format ?

mdml
  • 22,442
  • 8
  • 58
  • 66

6 Answers6

1

Date doesn't have a format at all. It is just the representation of time since the standard base time known as The Epoch, namely January 1, 1970, 00:00:00 GMT.

You can only get a String representation of the Date in the format required using the SimpleDateFormat.

Also, parse() method of SDF parses the String representation of the Date to a Date object. format() is the method which gives the formatted String representation of the Date object.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Forget about 'SimpleDateFormat' is there any option available to get Date object in any format? As I need to pass date object with particular format in external API? – user3097955 Dec 13 '13 at 04:05
  • @user3097955 - The very first line of my answer should answer your doubt. *`Date` does not have a format of its own*. – Rahul Dec 13 '13 at 04:06
1

Avoid legacy date-time classes

The old date-time classes bundled with the earliest versions of Java are poorly designed, confusing, and troublesome. Avoid them. Now supplanted by the java.time classes.

Instant

As others said, a java.util.Date represents a moment on the timeline in UTC with a resolution of milliseconds. A java.util.Date has no format at all.

You can convert Date objects to a java.time.Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = myUtilDate.toInstant();

ZonedDateTime

Assign a time zone in which getting a date makes sense in your scenario. A time zone is crucial to determining a date; for any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

LocalDate

Now if you want to work with date-only values without a time-of-day and without a time zone, extract a LocalDate object from the ZonedDateTime object.

LocalDate ld = zdt.toLocalDate();

Generating strings

To generate a String representing this LocalDate value in the format you requested, simply call toString. That format of YYYY-MM-DD is a standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating strings. Do not confuse a formatted string with the date-time object. A date-time object has no format, only strings have a format.

String output = ld.toString();

If you want other formats, search Stack Overflow for the class DateTimeFormatter.

About java.time

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

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

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

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

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.

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

No it is fixed in toString() method of Date class, You could extend Date if you strictly want this functionality

jmj
  • 237,923
  • 42
  • 401
  • 438
0

You could extend the java.util.Date class, and override the default toString() method. You might implement the Decorator pattern (or the Adapter pattern - I'm not certain if there's a distinction really).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The short answer to your question is no. The classes Date and GregorianCalendar don't include a format - you have to have the format separately, either as a String or a SimpleDateFormat.

If you really want to be able to pass an object which has both a date and a format, you could make your own class, which has a GregorianCalendar and a SimpleDateFormat as fields. You could give it a toString method that formats the GregorianCalendar using the SimpleDateFormat.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Your comments suggest your are missing the point that a date-time object is not a string. In both java.util.Date and Joda-Time, a date-time is represented as milliseconds since the Epoch. In Java 8 java.time.* the date-time may be nanoseconds since Epoch. But none of that has any thing to do with strings. We create strings as output, as a translation of those milliseconds/nanoseconds, to be meaningful to humans.

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