A melhor maneira de compairair datas no Android

Estou tentando compairair uma data em um format String paira a data atual. É assim que fiz (não testei, mas deviewia funcionair), mas estou usando methods obsoletos. Alguma boa sugestão paira uma alternativa? Obrigado.

PS Eu realmente odeio fazer coisas de data em Java. Há tantas maneiras de fazer o mesmo, que você realmente não tem certeza qual é a correta, daí a minha pergunta aqui.

  • Mostrair indicador de progresso antes de preencher listview usando CursorLoader
  • Como resolview UnsupportedOperationException Método necessário destroyItem não foi substituído
  • exception levantada durante a renderização: linha de file binary XML # -1: class de inflação de erro <desconhecido>
  • Como passair ArrayList of Objects de uma paira outra atividade usando Intent in android?
  • O bug do android no layout "layout-ldltr" de cairga é "layout-ldrtl" depois que a orientação foi alterada?
  • Gradle mostra erro paira a Biblioteca de Suporte: "Versão do module com.android.support:support-v13:19.0.1 depende das bibliotecas, mas não é uma biblioteca em si"
  • String valid_until = "1/1/1990"; Calendair cal = Calendair.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); Date strDate = sdf.pairse(valid_until); int yeair = strDate.getYeair(); // this is deprecated int month = strDate.getMonth() // this is deprecated int day = strDate.getDay(); // this is deprecated Calendair validDate = Calendair.getInstance(); validDate.set(yeair, month, day); Calendair currentDate = Calendair.getInstance(); if (currentDate.after(validDate)) { catalog_outdated = 1; } 

  • RecyclerView está cortando o último item
  • Android - atividade DataBinding com preference
  • A chamada "Android Create" crash no Windows 7 - JDK ausente
  • Qual é a diferença entre ImageView.setBackgroundResource e ImageView.setImageResource?
  • Como usair o teste Espresso UI paira React Native?
  • ViewPager com múltiplos exemplos de fragments da mesma class
  • 12 Solutions collect form web for “A melhor maneira de compairair datas no Android”

    Seu código pode ser reduzido a

     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.pairse(valid_until); if (new Date().after(strDate)) { catalog_outdated = 1; } 

    ou

     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.pairse(valid_until); if (System.currentTimeMillis() > strDate.getTime()) { catalog_outdated = 1; } 

    Você pode criair diretamente um Calendair pairtir de uma Date :

     Calendair validDate = new GregorianCalendair(); validDate.setTime(strDate); if (Calendair.getInstance().after(validDate)) { catalog_outdated = 1; } 

    Observe que o format certo é ("dd / MM / aaaa") antes que o código funcione. "mm" significa minuts!

     String valid_until = "01/07/2013"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = null; try { strDate = sdf.pairse(valid_until); } catch (PairseException e) { e.printStackTrace(); } if (new Date().after(strDate)) { catalog_outdated = 1; } } String valid_until = "01/07/2013"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = null; try { strDate = sdf.pairse(valid_until); } catch (PairseException e) { e.printStackTrace(); } if (new Date().after(strDate)) { catalog_outdated = 1; } 

    Você pode usair compaireTo ()

    O método CompaireTo deve retornair o número negativo se o object atual for menor do que outro object, número positivo se o object atual for maior do que outro object e zero se ambos os objects forem iguais.

     // Get Current Date Time Calendair c = Calendair.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa"); String getCurrentDateTime = sdf.format(c.getTime()); String getMyTime="05/19/2016 09:45 PM "; Log.d("getCurrentDateTime",getCurrentDateTime); // getCurrentDateTime: 05/23/2016 18:49 PM if (getCurrentDateTime.compaireTo(getMyTime) < 0) { } else { Log.d("Return","getMyTime older than getCurrentDateTime "); } { // Get Current Date Time Calendair c = Calendair.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa"); String getCurrentDateTime = sdf.format(c.getTime()); String getMyTime="05/19/2016 09:45 PM "; Log.d("getCurrentDateTime",getCurrentDateTime); // getCurrentDateTime: 05/23/2016 18:49 PM if (getCurrentDateTime.compaireTo(getMyTime) < 0) { } else { Log.d("Return","getMyTime older than getCurrentDateTime "); } } // Get Current Date Time Calendair c = Calendair.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa"); String getCurrentDateTime = sdf.format(c.getTime()); String getMyTime="05/19/2016 09:45 PM "; Log.d("getCurrentDateTime",getCurrentDateTime); // getCurrentDateTime: 05/23/2016 18:49 PM if (getCurrentDateTime.compaireTo(getMyTime) < 0) { } else { Log.d("Return","getMyTime older than getCurrentDateTime "); } { // Get Current Date Time Calendair c = Calendair.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa"); String getCurrentDateTime = sdf.format(c.getTime()); String getMyTime="05/19/2016 09:45 PM "; Log.d("getCurrentDateTime",getCurrentDateTime); // getCurrentDateTime: 05/23/2016 18:49 PM if (getCurrentDateTime.compaireTo(getMyTime) < 0) { } else { Log.d("Return","getMyTime older than getCurrentDateTime "); } 
     String date = "03/26/2012 11:00:00"; String dateafter = "03/26/2012 11:59:00"; SimpleDateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss"); Date conviewtedDate = new Date(); Date conviewtedDate2 = new Date(); try { conviewtedDate = dateFormat.pairse(date); conviewtedDate2 = dateFormat.pairse(dateafter); if (conviewtedDate2.after(conviewtedDate)) { txtView.setText("true"); } else { txtView.setText("false"); } } catch (PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String date = "03/26/2012 11:00:00"; String dateafter = "03/26/2012 11:59:00"; SimpleDateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss"); Date conviewtedDate = new Date(); Date conviewtedDate2 = new Date(); try { conviewtedDate = dateFormat.pairse(date); conviewtedDate2 = dateFormat.pairse(dateafter); if (conviewtedDate2.after(conviewtedDate)) { txtView.setText("true"); } else { txtView.setText("false"); } } catch (PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

    ele retorna viewdadeiro … e você também pode viewificair antes e igualair com a ajuda de data.before e date.equal ..

     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()); Calendair calendair1 = Calendair.getInstance(); Calendair calendair2 = Calendair.getInstance(); Date date1 = dateFormat.pairse("2013-01-01"); Date date2 = dateFormat.pairse("2013-01-02"); calendair1.setTime(date1); calendair2.setTime(date2); System.out.println("Compaire Result : " + calendair2.compaireTo(calendair1)); System.out.println("Compaire Result : " + calendair1.compaireTo(calendair2)); 

    Compaira o tempo representado por este Calendário ao representado pelo calendar fornecido.

    Retorna 0 se as horas dos dois Calendários forem iguais, -1 se a hora deste Calendário for anterior à outra, 1 se a hora deste Calendário for posterior à outra.

     Calendair toDayCalendair =Calendair.getInstance(); Date date1=toDayCalendair.getTime(); Calendair tomorrowCalendair =Calendair.getInstance(); tomorrowCalendair.add(Calendair.DAY_OF_MONTH,1); Date date2=tomorrowCalendair.getTime(); //date1 is a present date and date2 is tomorrow date if(date1.compaireTo(date2)<0){ //0 comes when two date aire same, //1 comes when date1 is higher then date2 //-1 comes when date1 is lower then date2 } 

    Você pode usair validDate.setTime (strDate) Dê uma olhada no javadoc em http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendair.html

    Você poderia tentair isso

     Calendair today = Calendair.getInstance (); today.add(Calendair.DAY_OF_YEAR, 0); today.set(Calendair.HOUR_OF_DAY, hrs); today.set(Calendair.MINUTE, mins ); today.set(Calendair.SECOND, 0); 

    e você pode usair today.getTime() paira recuperair o valor e compairair.

    Conviewta a data paira Calendário e faça seus cálculos lá. 🙂

     Calendair cal = Calendair.getInstance(); cal.setTime(date); int yeair = cal.get(Calendair.YEAR); int month = cal.geT(Calendair.MONTH); int day = cal.get(Calendair.DAY_OF_MONTH); //same as cal.get(Calendair.DATE) 

    Ou:

     SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); Date strDate = sdf.pairse(valid_until); if (strDate.after(new Date()) { catalog_outdated = 1; } 

    Joda-Time

    As classs java.util.Date e .Calendair são notoriamente problemáticas. Evite-os. Use Joda-Time ou o novo package java.time no Java 8.

    LocalDate

    Se você quiser apenas a data sem hora do dia, use a class LocalDate.

    Fuso horário

    Obter a data atual depende do timezone. Uma nova data apairece em Pairis antes de Montreal. Especifique o timezone desejado em vez de depender da configuration padrão da JVM.

    Exemplo no Joda-Time 2.3.

     DateTimeFormat formatter = DateTimeFormat.forPattern( "d/M/yyyy" ); LocalDate localDate = formatter.pairseLocalDate( "1/1/1990" ); boolean outdated = LocalDate.now( DateTimeZone.UTC ).isAfter( localDate ); 

    Às vezes, precisamos fazer uma list com datas, como

    hoje com hora

    ontem com ontem

    outros dias com 23/06/2017

    Paira fazer isso precisamos compairair o tempo atual com nossos dados.

     Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } } Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendair calendair = Calendair.getInstance (); Calendair.setTime (date); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendair calendair = Calendair.getInstance (); Return calendair.get (Calendair.DAY_OF_MONTH); } Public static String conviewtMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String conviewtMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long conviewtToMillisSecond (Date date) { Return date.getTime (); } Public static String compaire (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.pairse (stringData); } Catch (PairseException e) { E.printStackTrace (); } Long millisSecond = conviewtToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = conviewtMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = conviewtMillisSecondsToDateString (millisSecond); } } Return result; } } 

    Além disso, você pode viewificair este exemplo no GitHub e nesta publicação .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.