-4

My time format is coming SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

2108-03-27T17:18:16.985+0530

input Date I have to convert it into other time format

Mar 27,2018 5:18 pm

is expected output can any please suggest me how to convert given time to other time format in java .

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • 2
    Take a look at the Date and Time Patterns on https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html –  Mar 27 '18 at 12:51
  • 1
    Also take a look at the Date and Time API https://dzone.com/articles/deeper-look-java-8-date-and – NotZack Mar 27 '18 at 12:53
  • You can choose other date format instead convert given time to other time format. Here u have examples of other time formats. Try this: [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) –  Mar 27 '18 at 12:53
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 27 '18 at 20:25

4 Answers4

1
SimpleDateFormat sdf = new SimpleDateFormat("MMM d,yyyy h:mm a");
System.out.println(sdf.format(date));
Johan Claes
  • 139
  • 2
  • 14
1

If you have Java 8, use the java.time API:

DateTimeFormatter parser = new DateTimeFormatterBuilder()
                .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .appendPattern("XX")
                .toFormatter();
OffsetDateTime odt = OffsetDateTime.parse("2108-03-27T17:18:16.985+0530", parser);

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd,yyyy h:mm a", Locale.ENGLISH);
String formattedDate = fmt.format(odt); // Mar 27,2108 5:18 PM

SimpleDateFormat has lots of problems, many of them solved by java.time API, and you should prefer to use those.

For older versions of Java, there's a nice backport, with the same classes and functionality.

Jdit
  • 11
  • 1
0
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
    System.out.println(sdf.format(new Date()));

//Mar 27, 2018 06:28 PM

for more What are the date formats available in SimpleDateFormat class?

Roushan
  • 4,074
  • 3
  • 21
  • 38
0

You can convert type any-to-any of date format.

You just need to pass String format to SimpleDateFormate.

Use this method as static and call it from anywhere by passing inputDate

Simplified Date convert method:

public static String getFormattedDate(String inputDate)
{
    String outputFormattedDate = "";
    try
    {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z");
        Date inputFormatDate = inputFormat.parse(inputDate);

        SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yyyy h:mm a");
        outputFormattedDate = outputFormat.format(inputFormatDate);
    }
    catch (Exception ex)
    {
        outputFormattedDate = inputDate;
        ex.printStackTrace();
    }

    return outputFormattedDate;
}

Hope it will help you.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40