-1

if i wanted to compare to date objects like date1 and date2, not to check if they are the same date, but to check if the formatting (such as (YY:MM:SS) ) of the dates are the same, how could I go about doing so? I can only seem to find documentation on comparing the actual dates themselves, not the formatting.

for clarification, say i have dates 2017:07:20 and 2016:05:24, i want to see that they are of the same format... hope that helps

Sentinel
  • 98
  • 1
  • 11
  • A date has no format: https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/ –  Jul 20 '17 at 14:46

2 Answers2

0

You can't get the format from a Date object.

Because the date itself doesn't contain formatting information. Only if you have a date-string you could get the formatting like shown in this SO answer.

Christian
  • 22,585
  • 9
  • 80
  • 106
0

You cannot do that, such as format is only to print. Theyr are stored in a big number which allow to have precision from nanoseconds to size of era and can be represented as many differents ways

A comparison maybe easier to understand : it's the same as int, they are stored as binary and you can print them in decimal, octal, hexa ...


And as decimal format is by default for int, double,..., it's DateTimeFormatter.ISO_LOCAL_DATE_TIME which is by default for LocalDateTime

LocalDateTime date = LocalDateTime.now();
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
System.out.println(date);  // will print the same as line 2 
azro
  • 53,056
  • 7
  • 34
  • 70