I am little bit confused in dates. I am currently working on the weather app and everything works fine .. I just wanna handle this type of format into my own desirable format.
2017-09-10T18:35:00+05:00
I just wanna convert this date into Epoch Time and then I settle the date in my desire format ::
for J-SON
or i wanna convert this date into less figure i.e Sun , 9 september 9:23 Am etc.
http://dataservice.accuweather.com/currentconditions/v1/257072?apikey=JTgPZ8wN9VUy07GaOODeZfZ3sAM12irH&language=en-us&details=true
Use a SimpleDateFormat
instance to parse the string into a Date
object:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = parser.parse("2017-09-10T18:35:00+05:00");
And then use another SimpleDateFormat to display it:
DateFormat format = new SimpleDateFormat("EEE, dd MMMMM h:mm a");
String formatted = format.format(date); // Sun, 10 September 1:35 PM
ThreeTenABP
The other answers are correct, but outdated before they were written. These days I recommend you use the modern Java date and time API known as JSR-310 or java.time
. Your date-time string format is ISO 8601, which the modern classes “understand” as their default.
Can you use the modern API on Android yet? Most certainly! The JSR-310 classes have been backported to Android in the ThreeTenABP project. All the details are in this question: How to use ThreeTenABP in Android Project.
long epochTime = OffsetDateTime.parse("2017-09-10T18:35:00+05:00")
.toInstant()
.getEpochSecond();
The result is 1505050500.
Example of how to convert this into a human-readable date and time:
String formattedDateTime = Instant.ofEpochSecond(epochTime)
.atZone(ZoneId.of("Africa/Lusaka"))
.format(DateTimeFormatter.ofPattern("EEE, d MMMM h:mm a", Locale.ENGLISH));
This produces Sun, 10 September 3:35 PM
. Please provide the correct region and city for the time zone ID you want. If you want to rely on the device’s time zone setting, use ZoneId.systemDefault()
. See the documentation of DateTimeFormatter.ofPattern() for the letters you may use in the format pattern string, or use DateTimeFormatter.ofLocalizedDateTime()
for one of your locale’s default formats.
You can use SimpleDate formatter to parse you date as string into epoch
String input = "2017-09-10T18:35:00+05:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = sf.parse(input);
long dateInEpochFormatInMilliSeconds = date.getTime();
//if you want this in seconds then
long dateInEpochFormatInSeconds = date.getTime()/1000L;
//if you want to show only date month and year then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(dateInEpochFormatInMilliSeconds);
//This date String will contain the date in dd-MM-yyyy format
} catch (ParseException| ArithmeticException e) {
e.printStackTrace();
}
String time_at_which_weather_capture = "Time : ";
DateFormat dateFormat = new SimpleDateFormat("EEE,d M yyyy h:MM a");
long timeInMillieSec = 0 ;
try {
Date date = dateFormat.parse(readyToUpdate.getTime());
timeInMillieSec = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
time.setText(time_at_which_weather_capture + String.valueOf(time_fetcher(timeInMillieSec)));
public String time_fetcher (long time_coming_to_its_original_form) {
Date date = new Date (time_coming_to_its_original_form);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d M yyyy h:MM a");
return sdf.format(date);
}