-1

Server gives me time in this format:

 Tue Jan 10 03:48:45 GMT+05:30 2017"

I want to convert it into:

Tue,Jan 2017"

I have searched google for a while now, still no luck. How can I do this?

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
  • Use SimpleDateFormat for that. – Vladyslav Matviienko Jan 11 '17 at 11:21
  • try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH); Date myDate = simpleDateFormat.parse("Tue Jan 10 03:48:45 GMT+05:30 2017"); SimpleDateFormat outputFormat = new SimpleDateFormat("EEE,MMM yyyy", Locale.ENGLISH); System.out.println(outputFormat.format(myDate)); } catch (Exception e) { e.printStackTrace(); } – Deepak Goyal Jan 11 '17 at 11:39
  • Thanks @DeepakGoyal. It works, sorry for the late reply, was stationed in a remote area, no internet. – Joko NdiOha Feb 14 '17 at 02:41

1 Answers1

0

Following code will help you to convert one time format to another:

public static String formatDateFromOnetoAnother(String date) {
    String resultformat = "EEE,MMM dd";
    String givenformat = "EEE MMM dd zZ yyyy";
    String result = "";
    SimpleDateFormat sdf;
    SimpleDateFormat sdf1;
    try {
        sdf = new SimpleDateFormat(givenformat);
        sdf1 = new SimpleDateFormat(resultformat);
        result = sdf1.format(sdf.parse(date));
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    } finally {
        sdf = null;
        sdf1 = null;
    }

    return result;
}
Anjali
  • 1
  • 1
  • 13
  • 20