0

I have a String that formatted MM/dd. I would like to convert it to a Date in format yyyy-MM-dd'T'HH:mm:ss.SSZ.

DateFormat df = new SimpleDateFormat("MM/dd");
String strDate = "06/05";
Date date = new Date();
date = df.parse(strDate);

This makes it a Date, but in the original format.

System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSZ").format(date));

This returns the correct month and day, but nopthing else.

1970-06-05T00:00:00.00-0400

Any idea how I can make it return

CURRENT_YEAR-06-05TCURRENT_TIME
A.J
  • 1,140
  • 5
  • 23
  • 58
  • 1
    You said you have `MM-yy` date, yet you pass `MM/dd` to `SimpleDateFormat`. – Cromax Jun 30 '20 at 18:16
  • @Cromax sorry it was a type. Fixed it. – A.J Jun 30 '20 at 18:20
  • 1
    OK. There's still typo (`-` instead of `/`). – Cromax Jun 30 '20 at 18:20
  • 1
    Format `SS` will not work. `SimpleDateFormat` can only handle 3 S's, anything else will not work correctly. Did you mean to use `SSS`, or do you truly only want 2 decimals of fractional seconds? – Andreas Jun 30 '20 at 18:38
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `MonthDay`, `OffsetDateTime` and `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 01 '20 at 03:00

5 Answers5

3

In the question, the date format pattern indicates a desire for 2-digit fractional seconds. SimpleDateFormat cannot do that.

The newer Java 8 Time API can, and you should be using that anyway.

If you're running on Java 6 or 7, get the ThreeTen-Backport library.

To parse a MM/dd formatted string and get a full timestamp with current year and time-of-day, in the default time zone, use the following code:

String strDate = "06/05";
MonthDay monthDay = MonthDay.parse(strDate, DateTimeFormatter.ofPattern("MM/dd"));
ZonedDateTime date = ZonedDateTime.now().with(monthDay);
System.out.println(date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSZ")));

Sample Output

2020-06-05T14:52:48.45-0400
Andreas
  • 154,647
  • 11
  • 152
  • 247
2

I recommend to make use of java.time package. There you go:

var ds = "01/12";
var df = java.time.format.DateTimeFormatter.ofPattern("MM/dd");
var dt = java.time.MonthDay.from(df.parse(ds)).adjustInto(java.time.LocalDateTime.now());

Then you can convert dt to java.util.Date or whatever you like. Or simply use one of java.time formaters to get the desired output.

Cromax
  • 1,822
  • 1
  • 23
  • 35
1

For something this simple I suggest a different approach, get current time then set month and day from the original string THEN format.

String str = "08/09";
        String[] split = str.split("/");

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MONTH, Integer.parseInt(split[0]));
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(split[1]));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(calendar.getTime()));
Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
  • @Andreas How so ? You re wrong I think. He wants current year and time with provided date and month. Can you explain ??? – Ali Ben Zarrouk Jun 30 '20 at 18:29
  • 1
    `calendar.setTime(new Date());` is unnecessary, since `getInstance()` returns a `Calendar` object already initialized to current time. – Andreas Jun 30 '20 at 18:34
  • 1
    Format `SS` will not work. `SimpleDateFormat` can only handle 3 S's, anything else will not work correctly. --- *(problem copied from question, but not addressed in answer)* – Andreas Jun 30 '20 at 18:37
  • Great to know, that was just the line he had for time formatting, I thought he had it working fine. – Ali Ben Zarrouk Jun 30 '20 at 18:38
  • https://stackoverflow.com/questions/1459656/how-to-get-the-current-time-in-yyyy-mm-dd-hhmisec-millisecond-format-in-java This says otherwise btw – Ali Ben Zarrouk Jun 30 '20 at 18:41
  • 1
    I'm sorry, where in that other question does it say that `SimpleDateFormat` can handle 2 S's? The `S` pattern letter means **millisecond** regardless of how many letters are used. `SSS` means to zero-prefix to 3 digits, which is appropriate. `SS` means to zero-prefix to 2 digits, so e.g. 53 milliseconds is formatted as `53`, meaning formatted time becomes `23.59.59.53` which is **wrong**, since that means 530 milliseconds, not 53. – Andreas Jun 30 '20 at 18:47
  • I see, so shall we do only 2 S then append a 0 ? :) – Ali Ben Zarrouk Jun 30 '20 at 18:56
  • 1
    We ask OP ([already done](https://stackoverflow.com/questions/62663710/change-date-from-mm-dd-to-yyyy-mm-ddthhmmss-ssz-in-java/62663873?noredirect=1#comment110816888_62663710)) what the intent was. If the answer is "2 decimals", then it cannot be done with `SimpleDateFormat`, and should be done with `DateTimeFormatter`, as shown [here](https://stackoverflow.com/a/62664297/5221149). – Andreas Jun 30 '20 at 18:57
1

You are creating a date with only month and day If you want to use the current year and time, you can create a calendar object and edit the month and day

0
String strDate = "06-05";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-"+strDate+"'T'HH:mm:ss.SSZ");
System.out.println(sdf.format(new Date()));

the output:

2020-06-05T23:00:45.306+0400
Ridwan
  • 214
  • 4
  • 17