1

I need to formatt Calendar to get just the Date DD/MM/YY but no converted into String, i need it into Date dataType.

SimpleDateFormat simpleDate = new SimpleDateFormat("dd/M/yy");
Calendar cal2 = new GregorianCalendar(simpleDate);
 dateFormat.format(date); //Here i need to store the Date (dd/M/yy) into a Date kin of variable. (NO STRING)

Thus, when i need to update Calendar, into the Date result will reflect the changes.

  • Read this [question](http://stackoverflow.com/questions/25460965/how-can-i-create-a-date-object-with-a-specific-format), understand its answer and then you'll know how to achieve the same for your `Calendar` sub-class. – Tom Jun 27 '16 at 02:40

2 Answers2

-1

I do not know if this is close enough if someone with more experience can check this over that would be nice!

To truncate a combined "date and time" java.util.Date to just the date component, leaving it effectively at midnight

public static Date truncateTime (Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime( date);
cal.set( Calendar.HOUR_OF_DAY, 0);
cal.set( Calendar.MINUTE, 0);
cal.set( Calendar.SECOND, 0);
cal.set( Calendar.MILLISECOND, 0);
return cal.getTime();

}

Bradley Dale
  • 23
  • 1
  • 1
  • 7
-1

No such thing as formatted date

A date-time value stored in a date-time class has no format.

A date-time object can generate a String object whose text is in a certain format to represent the date-time’s value. But the String and the date-time are separate and distinct from one another.

java.time

The LocalDate class represents a date-only value, without time-of-day and without time zone.

A time zone is crucial in determining a LocalDate. For any given moment, the date varies around the globe by time zone. For example, a few minutes after midnight is still “yesterday” in Montréal.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );

Avoid using the troublesome old date-time classes such as java.util.Date and java.util.Calendar. Use only java.time classes.

Convert from GregorianCalendar

If you have a GregorianCalendar object, convert to a ZonedDateTime by calling the new method added to the old class, toZonedDateTime.

ZonedDateTime zdt = myGregCal.toZonedDateTime();

From there get a LocalDate. The ZonedDateTime object’s own assigned time zone is used to determine that date.

LocalDate localDate = zdt.toLocalDate();

Convert from java.util.Date

If you have a java.util.Date object, convert to an Instant. This class represents a moment on the timeline in UTC.

Instant instant = myUtilDate.toInstant();

Apply a time zone to get a ZonedDateTime. Then obtain a LocalDate as we saw above.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
LocalDate localDate = zdt.toLocalDate();

Immutable objects

The java.time classes follow the Immutable Objects design. Rather than alter (“mutate”) the value or attributes of a java.time object, a new java.time object is instantiated based on the attributes of the original object.

Generate String

When you want to generate a String to represent the LocalDate value, define a formatting pattern.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/M/yy" );
String output = localDate.format( formatter );

By the way, I strongly advise against the use of two-digit years. The ambiguity raised and issues involved with parsing are not worth the savings of two characters.

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