-4

I have a custom date string which is basically a concatenation of the last digit of the year, the number that corresponds to the month, the date, the hour, min, and seconds.
Sample data String: 171231120000

I wanted it to format to something like: 2017-12-31T12:00:00 but it seems unparseable.

Can you help check what's wrong with my code below?

String dateInString = "171231120000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-ddThh:mm:ss");     
Date date = sdf.parse(dateInString);

System.out.println(date);
Gozus19
  • 165
  • 19
yonan2236
  • 13,371
  • 33
  • 95
  • 141

2 Answers2

2

It seems you missed 'T' and wrote only T instead.

Check this Oracle documentation on SimpleDateFormat. You could try to substitute your code line to this one:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

UPDATE

Or this one:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMMddd'T'HHmmss");

Depending on your string. I'm sure that you didn't write the right T. Try this!

Let me know if this helps you!

UPDATE 2

Util method to parse your string into SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

public String gerRightFormat(String input) {
    String output = "";
    for (int i = 0; i < input.length(); i++) {
        if (i <= 4) {
            output += input.charAt(i);
        } else if (i >= 5 && i <= 6) {
            output += input.charAt(i);
        } else if (i >= 7 && i <= 9) {
            output += input.charAt(i);
        } else if (i >= 10 && i <= 12) {
            output += input.charAt(i);
        } else if (i >= 13 && i <= 15) {
            output += input.charAt(i);
        }
        if (i == 3) {
            output += "-";
        } else if (i == 5) {
            output += "T";
        } else if (i == 8) {
            output += ":";
        } else if (i == 11) {
            output += ":";
        }
    }
    return output;
}

Your output will be your date string in SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");. Should work!

Gozus19
  • 165
  • 19
  • still unparseable.... I made my date string to "20171231120000" and used both your suggested formatter... :( – yonan2236 Jul 13 '17 at 07:36
  • Try this `new SimpleDateFormat("yyyyMMddHHmmss");` – Gozus19 Jul 13 '17 at 07:37
  • it converted the string to date but it is not in the expected output... here's how it is displayed: Sun Dec 31 12:00:00 CST 2017 – yonan2236 Jul 13 '17 at 07:38
  • To parse a date is important that all characters given to `SimpleDateFormat`'s constructor match with your plain string. Be sure that every character has a corresponding value. Example, if you write `yyyyMM` your string cannot be `2017223`. In documentation you'll find your answer, by the way. I'm happy to help you. – Gozus19 Jul 13 '17 at 07:39
  • thank you... I also already read about that... but this legacy system that we use this is the format it sends to our system. – yonan2236 Jul 13 '17 at 07:40
  • In this case you have to change your plain string. Maybe you could create a util method that change your string value to something parseable in this `yyyy-MM-dd'T'HH:mm:ss`. Try to change your plain string to `1712-31T12:00:00` – Gozus19 Jul 13 '17 at 07:41
  • I'm thinking of other way but it is not elegant... I could extract the year, month, date, etc. values from the string and construct the date? – yonan2236 Jul 13 '17 at 07:41
  • You could use Java's `Calendar` to do that. It's great to manage `Date` objects. There you have documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html – Gozus19 Jul 13 '17 at 07:43
  • ok so I think I will go that route. gracias Manuel! – yonan2236 Jul 13 '17 at 07:43
  • use `long dateInString = 171231120000L;` instead `String dateInString = "171231120000";` – Sudha Velan Jul 13 '17 at 07:44
  • ok will try now – yonan2236 Jul 13 '17 at 07:44
  • You could create a method with a counter that formats your string into `yyyy-MM-dd'T'HH:mm:ss`. Try something like a `for` loop that checks string's `charAt(index)`. If index is <= 4 then write the year into the string. Once index is at `4` index you add `-`, when index is over 6 you have to add another `-`... Do this for all the string should make the trick! – Gozus19 Jul 13 '17 at 07:46
  • @SudhaVelan that will convert date in milliseconds but we don't need that. We need a right format to be parsed :) try my method yonan! – Gozus19 Jul 13 '17 at 07:47
  • I just tried 171231120000L. it works when my formatter is yyyyMMddHHmmss. but still the output is not in the expected format: Sun Dec 31 12:00:00 CST 2017. I guess I need to extract the year, month, date, etc. from this date and reconstruct the date – yonan2236 Jul 13 '17 at 07:48
  • I update my answer with the util method @yonan2236 – Gozus19 Jul 13 '17 at 07:57
  • ok will try now – yonan2236 Jul 13 '17 at 08:02
  • do I need to just supply my dateSTring to the method? – yonan2236 Jul 13 '17 at 08:12
  • it says unparseable... here's how my codes look like: `String dateInString = "171231120000"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date date = sdf.parse(gerRightFormat(dateInString)); System.out.println(date);` – yonan2236 Jul 13 '17 at 08:14
  • I think got it but there must be some error in the logic, here's the output of your util method: 20171-23T112:000:0 I will try to update your code/ – yonan2236 Jul 13 '17 at 08:17
  • check indexes of util method and try to reduce them by 1. I didn't test method. You can try to fix index, then should work fine :) sorry for this, I wrote it rapidly – Gozus19 Jul 13 '17 at 08:18
  • I think to have found error. In the second if block reduce all indexes from 1. `if (i == 3) { //do something } else if (i == 5) { ...` do this for all if-else from `if (i == 4)` :) – Gozus19 Jul 13 '17 at 08:22
  • I did what you say, it appears the output is still broken: 2017-12T311:200:00. – yonan2236 Jul 13 '17 at 08:30
  • I think I can take it from here. I will figure this out. thank so far your help :) – yonan2236 Jul 13 '17 at 08:31
  • No problem, it's a pleasure! If this helps you to solved the problem, please, set the answer as solved. Thanks :) – Gozus19 Jul 13 '17 at 08:35
  • @yonan2236 Why is this Answer accepted? It discusses a formatting pattern unrelated to the input string. See [correct Answer by Titus](https://stackoverflow.com/a/45074181/642706). – Basil Bourque Jul 15 '17 at 02:32
0

That is because the format of your date string is not yyyy-mm-ddThh:mm:ss you should use a format that looks something like this yyMMddhhmmss

Titus
  • 22,031
  • 1
  • 23
  • 33