2017-09-23 5 views
-1

Ich arbeite an einer Nachrichten App und ich bekomme das Datum und die Uhrzeit von der JSON-Antwort in diesem Muster (2017-09-23T14:22:28Z).Datum und Uhrzeit Umwandlung

Wie kann ich dieses Datum und diese Uhrzeit in so etwas umwandeln (vor 5 Sekunden, 10 Minuten, 3 Stunden usw.)? Und nach 24 Stunden, sollte es dann das normale Datum dieser bestimmten Schlagzeile anzeigen (2017-09-23).

Antwort

0

Sie können diesen Code verwenden, den ich in meiner Chat-App implementiert habe.

public class Utils 
{ 
    private static final int SECOND_MILLIS = 1000; 
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS; 
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS; 
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS; 


    public static String getTimeAgo(long time, Context ctx) { 
     if (time < 1000000000000L) { 
      // if timestamp given in seconds, convert to millis 
      time *= 1000; 
     } 

     long now = getCurrentTime(ctx); 
     if (time > now || time <= 0) { 
      return null; 
     } 

     // TODO: localize 
     final long diff = now - time; 
     if (diff < MINUTE_MILLIS) { 
      return "just now"; 
     } else if (diff < 2 * MINUTE_MILLIS) { 
      return "a minute ago"; 
     } else if (diff < 50 * MINUTE_MILLIS) { 
      return diff/MINUTE_MILLIS + " minutes ago"; 
     } else if (diff < 90 * MINUTE_MILLIS) { 
      return "an hour ago"; 
     } else if (diff < 24 * HOUR_MILLIS) { 
      return diff/HOUR_MILLIS + " hours ago"; 
     } else if (diff < 48 * HOUR_MILLIS) { 
      return "yesterday"; 
     } else { 
      return diff/DAY_MILLIS + " days ago"; 
     } 
    } 
} 

Es so viel von 3rd-Party-Bibliotheken, aber Sie können auch verweisen: https://github.com/curioustechizen/android-ago

0

Sie JodaTime Bibliothek betrachten können alle Datum, Uhrzeit und Kalender verwalten zu können! Hier ist ein Code von Joda Time.

public boolean isAfterPayDay(DateTime datetime) { 
     if (datetime.getMonthOfYear() == 2) { // February is month 2!! 
     return datetime.getDayOfMonth() > 26; 
    } 
    return datetime.getDayOfMonth() > 28; 
    } 

    public Days daysToNewYear(LocalDate fromDate) { 
     LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1); 
     return Days.daysBetween(fromDate, newYear); 
    } 

    public boolean isRentalOverdue(DateTime datetimeRented) { 
     Period rentalPeriod = new Period().withDays(2).withHours(12); 
     return datetimeRented.plus(rentalPeriod).isBeforeNow(); 
    } 

    public String getBirthMonthText(LocalDate dateOfBirth) { 
     return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH); 
    } 

Überprüfen Sie die Bibliothek Android Specific oder Java Specific und deren Dokumentation HERE

0
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); 
Date stringDate = simpledateformat.parse(aDate, 0); 
DateUtils.getRelativeDateTimeString(this,d.getTime(), DateUtils.MINUTE_IN_MILLIS) 
+0

Das ist es? Nichts anderes ?? – mani

0

Joda-Time ist sehr schön für diese.

compile 'joda-time:joda-time:2.9.9' 


String dateTime = "2017-09-21T14:22:28Z"; 
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ"); 
    DateTime myBirthDate = format.parseDateTime(dateTime); 
    DateTime now = new DateTime(); 
    Period period = new Period(myBirthDate, now); 
    PeriodFormatter formatter = new PeriodFormatterBuilder() 
      .appendSeconds().appendSuffix(" Sekonds ago\n") 
      .appendMinutes().appendSuffix(" Minutes ago\n") 
      .appendHours().appendSuffix(" Hours ago\n") 
      .appendDays().appendSuffix(" Days ago\n") 
      .appendWeeks().appendSuffix(" Weeks ago\n") 
      .appendMonths().appendSuffix(" Months ago\n") 
      .appendYears().appendSuffix(" Years ago\n") 
      .printZeroNever() 
      .toFormatter(); 
    if (period.getDays()<1) { 
     String result = formatter.print(period); 
     System.out.println(result); 
    } else { 
     DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd"); 
     String result = format24hMore.print(myBirthDate); 
     System.out.println(result); 
    } 

EDIT Create-Methode

public String convertDate(String date) { 

    String result = ""; 
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ"); 
    DateTime from = format.parseDateTime(date); 
    DateTime now = new DateTime(); 
    Period period = new Period(from, now); 
    PeriodFormatter formatter = new PeriodFormatterBuilder() 
      .appendSeconds().appendSuffix(" Sekonds ago\n") 
      .appendMinutes().appendSuffix(" Minutes ago\n") 
      .appendHours().appendSuffix(" Hours ago\n") 
      .appendDays().appendSuffix(" Days ago\n") 
      .appendWeeks().appendSuffix(" Weeks ago\n") 
      .appendMonths().appendSuffix(" Months ago\n") 
      .appendYears().appendSuffix(" Years ago\n") 
      .printZeroNever() 
      .toFormatter(); 
    if (period.getDays()<1) { 
     result = formatter.print(period); 
    } else { 
     DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd"); 
     result = format24hMore.print(from); 

    } 
    return result; 
} 

und Sie können diese Methode aufrufen

System.out.println(convertDate("2017-09-21T14:22:28Z")); 
+0

Wie kann ich Ihren Code in meiner Android-App anwenden – mani

+0

@mani wenden Sie diese Methode wie in EDIT ich gemacht, diese Methode verdeckt nur Zeichenfolge aus Ihrer JSON-Antwort, wenn die Antwort in diesem Muster ist "2017-09-23T14: 22: 28Z" –

+0

welche klassen sollte ich in mein projekt importieren ?? – mani