-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.
  • 76,217
  • 14
  • 120
  • 142
  • 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

4

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.

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
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
  • 998
  • 8
  • 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);



}