user3097955
user3097955

Reputation: 11

Get Date type object in format in java

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 ?

Upvotes: 1

Views: 1434

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 338276

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.

Upvotes: 1

Basil Bourque
Basil Bourque

Reputation: 338276

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.

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

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.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201419

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).

Upvotes: 0

Rahul
Rahul

Reputation: 45060

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.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

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

Upvotes: 0

Related Questions