0

I'm getting the above Date format from our webservice. I have an idea on how to format the date, im just having issues with the fact it comes down as a string.

I have tried this but I need to return it as a String, which in a way isn't a problem.

This is what I have tried but it throws an Exception:

java.text.ParseException: Unparseable date: "2016-02-26T00:00:00+02:00" (at offset 4)

Code:

  public static String formatDate(String unFormattedTime) {
    String formattedTime;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");
      Date date = sdf.parse(unFormattedTime);

      formattedTime = sdf.format(date);
      return formattedTime;

    } catch (ParseException e) {
      e.printStackTrace();
    }

    return "";
  }

How could I format it in a format like dd MMM HH:mm?

Community
  • 1
  • 1
Stillie
  • 2,647
  • 6
  • 28
  • 50
  • 1
    If you want a string, then use a new SimpleDateFormat class to reformat the timestamp. Don't use the getTime method to get the long. That link you posted tells you how to parse one string into a date object – OneCricketeer Feb 19 '16 at 08:02
  • And you need to get the correct formatting to avoid your current error. – OneCricketeer Feb 19 '16 at 08:03
  • you could substitude your ":" and "+" before you format your String. Also why do you get a String representation from your webService cant you grab the calendar or date Object itself? – Tom Wellbrock Feb 19 '16 at 08:03
  • Just use *DateFormat DateFormat df = new SimpleDateFormat("dd MMM HH:mm");* – viratpuar Feb 19 '16 at 08:04
  • Maybe you should deal with the String. – hongbochen Feb 19 '16 at 08:08

2 Answers2

3

Here you are with a working snippet of what you want to achieve:

public class FormatDateExample {
    public static void main(String[] args) {
        String date =  "2016-02-26T00:00:00+02:00";
        System.out.println(formatDate(date));

    }

    public static String formatDate(String unFormattedTime) {
         String formattedTime;
         try {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
             Date date = sdf.parse(unFormattedTime);

             sdf = new SimpleDateFormat("dd MMM HH:mm");
             formattedTime = sdf.format(date);

             return formattedTime;

        } catch (ParseException e) {
             e.printStackTrace();
        }

        return "";
    }
}

First you have to parse the date with the given format you have

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);

Then you have to format that date to the desired format "dd MMM HH:mm"

sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);
pleft
  • 7,567
  • 2
  • 21
  • 45
  • That worked like a charm!!! it seems i just had it the wrong way around, formatting then parsing. thanks a mill – Stillie Feb 19 '16 at 08:16
1

If you are using Java 8 you can try this:

LocalDate parsedDate = LocalDate.parse(unFormattedTime, ISO_OFFSET_DATE_TIME)

Ref: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME