-3

I have a date object which, if I print, gives me Mon Feb 11 01:21:00 CST 2019. I want to convert this to a 24 HRS date Format. I do not want strings as output or input. Please try to keep the input and output variables as Date object only.

Edit : I want to further use the converted date To use Collections.sort on an Arraylist. I am able to do so in the present implementation as well, but In here 12:55 AM is being ordered way later than 1:15 AM for the same date, which should otherwise be ordered before it.

I am using Java 6. and am overriding compare method from the comparator to do the two parameters based sorting of which date is the second parameter.

Edit 2 : It is different because I want the output as a date Object and not as a string Object. Converting it to a string, using SimpleDateFormat, there are 'n' number of ways to do it but I want the output as a Date object so as to do the sorting and as I mentioned I am running on java 6.

benignfoppery
  • 55
  • 1
  • 7
  • 1
    what is the use of formatting date here? – Ashok Kumar N Feb 11 '19 at 11:47
  • `Date` doesn't have a format. You shouldn't use the output produced by `toString()`; it is misleading. Please [edit] some of the relevant code in your question so we can better help you. – TiiJ7 Feb 11 '19 at 11:55
  • Wanted to further use the converted date To use Collections.sort on an Arraylist. I am able to do so in the present implementation as well, but In here 12:55 AM is being sorted way later than 1:15 AM for the same date, which should otherwise be ordered before it @ashok-kumar-n – benignfoppery Feb 11 '19 at 12:02
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `ZonedDateTime` or another class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 11 '19 at 12:15
  • While you shouldn’t want to use `Date`, sorting by the natural order of `Date` objects should give you chronological order. If you observe something different, you may want to post the code that shows the surprising behaviour. [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ole V.V. Feb 11 '19 at 12:16
  • 1
    @ole-v-v I wouldn't wanna use it but the existing system has it and I just want to sort the existing list. – benignfoppery Feb 11 '19 at 12:19
  • “I do not want strings as output” – That makes no sense. Date-time objects do not have a “format”, only strings representing the value of a date-time object have a format. You are conflating the textual representation of a date-time object with the date-time object itself. – Basil Bourque Feb 11 '19 at 19:33

1 Answers1

0

The format of the java.util.Date.toString() method cannot be changed. The Javadoc states:

public String toString()

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

However, this is already in 24-hour format, as the Javadoc states a little bit further down:

  • hh is the hour of the day (00 through 23), as two decimal digits.
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Indeed, while the Question is poor and a duplicate of many many other Questions, the Answer is correct. Here's my upvote to compensate for the incorrect down-vote. – Basil Bourque Feb 11 '19 at 19:34