-2

Below is my code but it throws an exception saying that unable to parse date format.

     try{

        DateFormat outputFormat = new SimpleDateFormat("yyyyMMddHHmmss zzzz", Locale.getDefault());
        DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.getDefault());

        String inputText = channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
        Date date = inputFormat.parse(inputText);
        String outputText = outputFormat.format(date);

      } catch (Exception e) {
         e.printStackTrace();
      }
Nitesh Rathod
  • 368
  • 3
  • 15
  • `20180805231400 -0300` is not a TimeStamp its a Format. TimeStamp usually called for epoch time . – ADM Aug 10 '18 at 12:04
  • 1
    your inputFormat should be outputFormat and vice versa – Vladyslav Matviienko Aug 10 '18 at 12:06
  • make sure `channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();` is return correct value – Shubham Vala Aug 10 '18 at 12:08
  • @ShubhamVala it is returning 20180805231400 -0300 value as String. – Nitesh Rathod Aug 10 '18 at 12:21
  • check my edited answer – Jay Mungara Aug 10 '18 at 13:35
  • Word usage: a [timestamp](https://en.wikipedia.org/wiki/Timestamp) is “information identifying when a certain event occurred”, so yes, the string in the title serves well as a timestamp. – Ole V.V. Aug 10 '18 at 18:55
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 10 '18 at 18:55
  • `OffsetDateTime.parse("20180805231400 -0300", DateTimeFormatter.ofPattern("uuuuMMddHHmmss xx", Locale.getDefault())).format(DateTimeFormatter.ofPattern("uuuu-MM-dd-HH:mm:ss", Locale.getDefault()))` gives `2018-08-05-23:14:00`. – Ole V.V. Aug 10 '18 at 19:05

2 Answers2

0

I hope you can do it like this,

    oldfmt = "yyyyMMddHHmmss zzzz";
    newfmt = "HH:mm:ss";
    getTimeFromUtc(oldfmt, newfmt, "20180805231400 -0300");

public void getTimeFromUtc(String oldFormat, String newformat, String datetime) {
    SimpleDateFormat inputFormat = new SimpleDateFormat(oldFormat);
    inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat outputFormat = new SimpleDateFormat(newformat, Locale.getDefault());
    TimeZone defaultTimezone = TimeZone.getDefault();
    outputFormat.setTimeZone(defaultTimezone);
    String inputDateStr = datetime;
    Date date = null;
    try {
        date = inputFormat.parse(inputDateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (date != null) {
        outputDateStr = outputFormat.format(date);
    }
    Toast.makeText(getApplicationContext(), outputDateStr, Toast.LENGTH_LONG).show();
}
Jay Mungara
  • 6,663
  • 2
  • 27
  • 49
0

Try this,

try{

 DateFormat inputFromat = new SimpleDateFormat("yyyyMMddHHmmss zzzz", Locale.getDefault());
 DateFormat outputFormat= new SimpleDateFormat("HH:mm:ss", Locale.getDefault());

 String inputText = channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
                Date date = inputFormat.parse(inputText);
                String outputText = outputFormat.format(date);
} catch (Exception e) {
        e.printStackTrace();
}
Shubham Vala
  • 1,024
  • 7
  • 18