-1

I have a date field in the input as below

oldDate = 12-FEB-23

I want to convert this to

newDate = Sun Feb 13 05:30:00 IST 2023

using java 8 . Please help Both oldDate and newDate are of Date type.

We dont want to use the java.util.Date class.

String oldDate = "13-FEB-23"
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date newDate = formatter.parse(oldDate);

With this, we are getting Sun Feb 13 00:00:00 IST 2023. So, we are setting hours to this as newDate.setHours(5), but this is deprecated. So, we don’t want to use it anymore.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
chand
  • 27
  • 5
  • 1
    Unless it is a legacy requirement you would be much better off using `LocalDateTime` from the `java.time` package as `Date` and it's supporting methods are deprecated and buggy. – WJS Feb 17 '23 at 13:25
  • Please clarify. If you mean `java.util.Date`, then you are right that it prints like `Sun Feb 13 05:30:00 IST 2023`, but not like `12-FEB-23`. Also if it was already a `Date` and you wanted it converted to `Date`, there would be no conversion to do. So what is `oldDate`?? – Ole V.V. Feb 17 '23 at 13:30
  • As @WJS said, avoid the need to a `java.util.Date` if there is any way you can. That class was poorly designed and is long outdated. Depending on your situation and requirements you may want to use `LocalDate`, `ZonedDateTime` and/or other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Feb 17 '23 at 13:33
  • 1
    By IST do you mean India Standard Time? Other interpretations exist. And which RFC does your title refer to? They have got numbers. And Java has not got any RFC date time type, so I am worried that you may be asking the impossible. – Ole V.V. Feb 17 '23 at 13:35
  • 1
    None of the [Java] [date-time types](https://docs.oracle.com/javase/tutorial/datetime/index.html) have a format. The format is used when you want to display a string representation of that type. As mentioned in the other [comment](https://stackoverflow.com/questions/75484763/convert-date-to-rfc-date-time#comment133183235_75484763), the default string representation of a `java.util.Date` object is as shown in your question, i.e. `Sun Feb 13 05:30:00 IST 2023`. So your question doesn't really make sense. – Abra Feb 17 '23 at 13:45
  • Yeah. We dont want to use java.util.Date package. Date oldDate = "13-FEB-23" DateFormat formatter       = new SimpleDateFormat("dd-MMM-yy"); Date       newDate        = formatter.parse(oldDate); with this, we are getting Sun Feb 13 00:00:00 IST 2023 So, we are setting hours to this as newDate.setHours(5) but this is deprecated. So, we dont want to use it anymore. – chand Feb 17 '23 at 14:35
  • Surely that must be `String oldDate = "13-FEB-23";`?? Otherwise your code can’t compile. So `String`, not `Date` here. Please take care and be precise. It’s not fair that people who are trying to help you should guess and ask you repeatedly before the question gets clear. – Ole V.V. Feb 17 '23 at 14:46
  • I understand you don’t want a `java.util.Date`. That’s nice. So will you accept a `LocalDate`? A `ZonedDateTime`? An `Instant`? What are your requirements? `LocalDate` seems most natural to me. Then you neither need to care about time of day nor time zone nor UTC offset. – Ole V.V. Feb 17 '23 at 14:49
  • I still don’t get all of what you are trying to do. Some steps of it may be `LocalDate .of(2023, Month.FEBRUARY, 13) .atStartOfDay(ZoneOffset.UTC) .withZoneSameInstant(ZoneId.of("Asia/Kolkata"))`. Which yields `2023-02-13T05:30+05:30[Asia/Kolkata]`. See also [Java 8 DateTimeFormatter for month in all CAPS not working \[duplicate\]](https://stackoverflow.com/questions/33520491/java-8-datetimeformatter-for-month-in-all-caps-not-working). – Ole V.V. Feb 18 '23 at 04:39

1 Answers1

1

Apparently you want to:

  1. Parse a string representing a date.
  2. Determine the first moment of the day an that date as seen in a particular time zone.

First, parsing.

The trick here is that all caps for a month abbreviation does not fit the cultural norms of any locale I know of. So we must build a custom formatter that ignores case.

DateTimeFormatter f =
        new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern( "dd-MMM-uu" )
                .toFormatter( Locale.US );

Do the parsing, to produce a LocalDate object representing the date alone.

String input = "12-FEB-23";
LocalDate localDate = LocalDate.parse( input , f );

Specify your desired time zone. IST is not a real time zone, so I do not know what you meant exactly. I suppose from your example offset of five and half hours ahead of UTC that you meant Asia/Kolkata rather than Europe/Dublin.

ZoneId z = ZoneId.of( "Asia/Kolkata" );

Determine the first moment of the day on that date in that zone. Never assume the day starts at 00:00. Some dates in same zones may start at another time such as 01:00. Let java.time determine the start.

ZonedDateTime zdt = localDate.atStartOfDay( z );

Sun Feb 13 05:30:00 IST 2023

If you mean to generate text in that format, define a formatter to suit your taste. Use the DateTimeFormatter class. Search Stack Overflow to learn more as this has been covered many many times already.

RFC Date time

If you meant a Request For Comments, you will need to specify which one.

If you meant RFC 1123, your example is incorrect, missing the comma after the day of the week.

String output = zdt.format( DateTimeFormatter.RFC_1123_DATE_TIME ) ; 

See code run at Ideone.com.

Sun, 12 Feb 2023 00:00:00 +0530

By the way, this format is outmoded. Modern protocols adopt ISO 8601 for date-time formats in text.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Likely the OP meant [`DateTimeFormatter.RFC_1123_DATE_TIME`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME), although their example is missing the comma after the abbreviated day name and the time zone is placed before the year instead of after. As far as I'm aware, there is no RFC with a date format as shown in the question. – Mark Rotteveel Feb 20 '23 at 12:38
  • @MarkRotteveel I added some code for that, in response to your Comment. But RFC 1123 does not include the “IST”, only an offset. – Basil Bourque Feb 20 '23 at 17:04
  • The RFC 1123 format includes offsets and a limited number of zone IDs. The Java `RFC_1123_DATE_TIME` only supports GMT and offsets, but the RFC 1123 definition (which modifies the [RFC 822 definition](https://www.rfc-editor.org/rfc/rfc822#section-5) lists a number of zones, but IST is not one of them). – Mark Rotteveel Feb 21 '23 at 13:16