How to get time in format HH:mm:ss from timestamp 20180805231400 -0300 [duplicate]
Clash Royale CLAN TAG#URR8PPP
How to get time in format HH:mm:ss from timestamp 20180805231400 -0300 [duplicate]
This question already has an answer here:
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();
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
20180805231400 -0300
your inputFormat should be outputFormat and vice versa
– Vladyslav Matviienko
Aug 10 at 12:06
make sure
channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
is return correct value– Shubham Vala
Aug 10 at 12:08
channelListArrayList.get(0).getTv().getProgramme().get(j).getStart();
@ShubhamVala it is returning 20180805231400 -0300 value as String.
– Nitesh Rathod
Aug 10 at 12:21
check my edited answer
– Jay Mungara
Aug 10 at 13:35
2 Answers
2
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();
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();
20180805231400 -0300
is not a TimeStamp its a Format. TimeStamp usually called for epoch time .– ADM
Aug 10 at 12:04