-2

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Joe C Sep 10 '17 at 16:01

4 Answers4

5

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.

Edit: Arvind Kumar Avinash correctly points out in a comment: You do not need to convert an OffsetDateTime to an Instant to get the epoch seconds. You can simply use OffsetDateTime#toEpochSecond.

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • and if i solve this epochTime using Date , SimpleDateFormat will i get the correct Date.. ? – Saad Zahoor Sep 10 '17 at 16:19
  • because i tried other method and i keep getting date like Thu, 1 ,1 1970 5:01 Am – Saad Zahoor Sep 10 '17 at 16:19
  • 1
    Don’t do that. The point is to avoid the outdated classes, including `Date` and `SimpleDateFormat`. – Ole V.V. Sep 10 '17 at 16:19
  • then how i am supposed to convert this 1505050500 into real date or time – Saad Zahoor Sep 10 '17 at 16:24
  • it gives usage of API documented @Since 1.8+ ? – Saad Zahoor Sep 10 '17 at 16:41
  • Sorry, I gave you links to the wrong version of the API documentation. It should work, though. Still better, use the proper [TheeTen Backport API docs](http://www.threeten.org/threetenbp/apidocs/). @SaadZahoor – Ole V.V. Sep 10 '17 at 17:21
  • 1
    Good Answer, but `OffsetDateTime.parse` would be more appropriate than `ZonedDateTime.parse`. – Basil Bourque Sep 10 '17 at 20:06
  • Thanks, @BasilBourque, I don’t know how `ZonedDateTime` slipped through there, you are certainly correct. Fixed. – Ole V.V. Sep 11 '17 at 05:00
  • 1
    You do not need to convert an `OffsetDateTime` to an `Instant` to get the epoch seconds. You can simply use [`OffsetDateTime#toEpochSecond`](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html#toEpochSecond--). – Arvind Kumar Avinash Dec 09 '22 at 18:56
  • @ArvindKumarAvinash Correct, thank you. I have pasted your helpful comment into the answer. – Ole V.V. Dec 10 '22 at 12:52
0

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
Shadowfacts
  • 1,038
  • 10
  • 22
  • i appreciate your time , would you guide me the date in string (input) i want that date and display only few things i mean month date and time . – Saad Zahoor Sep 10 '17 at 15:54
  • I'm sorry, I'm not sure what you mean. Do you want to extract the month, day, and time from the input date? – Shadowfacts Sep 10 '17 at 15:56
  • the date above i retrieve is like 2017-09-10T18:35:00+05:00 when i show this date in my app its look bad to me so i want the date to be look like d i.e Sun, 10 September 9:40 am etc .. – Saad Zahoor Sep 10 '17 at 16:03
  • Thanks that what exactly i want .. ! – Saad Zahoor Sep 11 '17 at 15:31
0

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();
        }
Shriyansh Gautam
  • 1,084
  • 1
  • 7
  • 13
0
    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);



}