按日期收集。排序不适用于所有设备

时间:2019-09-02 18:03:48

标签: java android

我正在尝试使用Collection.sort使用Dates以ASC顺序对一些数组进行排序;但是,当我在几种设备上进行测试时,它只能在平板电脑上正常工作,而不能在手机上工作。平板电脑Android版本为8.1,手机版本为5.0.1,另一版本为9.0。

当我比较日期AS字符串时,

collection.sort()在每个设备上都可以正常工作,但是它不会重新排列我需要的方式,例如:我需要按升序排列的客户列表,如:30/08/2019 ,31/08/2019,02/09/2019,但比较String可以做到:02/09/2019,30/08/2019,31/08/2019。我希望我的解释是可以理解的

// clientsParent模型中需要排序的巫婆中有多个数组

  ClientModel client = new ClientModel();
  client.setFechaGestion(Utilities.convertStringToDate("02/09/2019 02:20:00 PM"));

  clientsParent.get(parentIndex).getClients().add(client);

  Collections.sort(clientsParent.get(parentIndex).getClients(), (c1, c2) -> 
  c1.getFechaGestion().compareTo(c2.getFechaGestion()));



adapter.notifyDataSetChanged();  

这是我的实用程序类中的方法,在该方法中,我将Web服务的JSON给出的字符串转换为更精确的排序。

  public static Date convertStringToDate(String date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertedDate;
    }

编辑

通过阅读评论并研究这确实是一个 Regional Setting 问题,如果您的项目中存在此类问题,那么您唯一要做的就是添加Locale作为第二个参数,如果在默认列表中找不到语言环境,则可以使用其构造函数实例化一个语言环境,例如:

  Locale localSpanish = new Locale("es", "ES");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa", localSpanish);
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertedDate;

P.D。:如本文所回答,请尝试不使用Date和SimpleDateFormat

1 个答案:

答案 0 :(得分:3)

tl; dr

  • 从不使用DateSimpleDateFormat
  • LocalDateTime是适合您的课程,因为您输入了日期和时间,但没有时区或UTC偏移量
  • 在您的课程上实施Comparable接口,或将Comparator传递给Collections.sort
  • 要在您的类上实现compareTo方法,只需让LocalDateTime对象进行比较即可,因为该类实现了compareTo
    this.when.compareTo( that.when )

ISO 8601

  

“ 2019年2月9日下午2:20:00”

当将日期时间值作为文本交换时,请始终使用标准的ISO 8601格式。使用24-hour clock而不是AM / PM。

例如:`2019-09-02T14:20:00“

  

我从Web服务转换JSON给定的字符串

ISO 8601的好处教育该Web服务的发布者。

避免使用旧的日期时间类

您正在使用糟糕的日期时间类。几年前,采用定义现代 java.time 类的JSR 310取代了这些方法。

请勿使用SimpleDateFormatDate

智能对象,而不是哑字符串

  当我比较日期AS字符串时,

collection.sort()在每个设备上都能正常工作

否,请勿将日期时间值存储为文本。我们有这个课程。

LocalDateTime

由于您没有使用ISO 8601格式,因此请定义格式模式以匹配输入字符串。

请注意,我们如何通过Locale来指定翻译AM / PM时要使用的人类语言和文化规范。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu hh:mm:ss a" ).withLocale( Locale.US ) ;

解析字符串。

LocalDateTime ldt = LocalDateTime.format( input , f ) ;

使用LocalDateTime作为类成员字段的类型,而不是StringLocalDateTime对象已经实现Comparable,知道如何排序。

请注意, LocalDateTime并不是不是代表时刻。缺少时区或偏移量的上下文,它仅存储日期和时间。如果要跟踪时间轴上的特定点,则应使用InstantOffsetDateTimeZonedDateTime。当我们希望保持每天的特定时间时,LocalDateTime仅用于预订将来的约会,而不论政客如何更改该地区使用的偏移量。

Table of date-time types in Java, both modern and legacy.

示例应用

简单的例子。不一定足够坚固以用于生产用途。

定义包含LocalDateTime对象的类。

class Event implements Comparable< Event > {
    // Member field variable.
    public LocalDateTime when ;

    // Constructor
    public Event( LocalDateTime localDateTime ) {
        this.when = localDateTime ;
    }

    // Implements `Comparable` interface.
    @Override
    public int compareTo( Event that ) {
        return this.when.compareTo( that.when ) ;
    }

    // Override `Object.toString` for better reporting of the value of this object.
    @Override
    public String toString() {
        return this.when.toString() ;
    }
}

main方法中的用法示例。

import java.util.*;
import java.lang.*;
import java.io.*;

import java.time.* ;
import java.time.format.* ;
import java.time.temporal.* ;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List< Event > events = new ArrayList<>( 3 ) ;
        events.add( new Event( LocalDateTime.of( 2019 , 3 , 14 , 12 , 0 , 0 , 0 ) ) ) ;
        events.add( new Event( LocalDateTime.of( 2019 , 1 , 17 , 14 , 0 , 0 , 0 ) ) ) ;
        events.add( new Event( LocalDateTime.of( 2019 , 2 , 21 , 17 , 0 , 0 , 0 ) ) ) ;
        System.out.println("Before: " + events ) ;

        Collections.sort( events ) ;
        System.out.println("After: " + events ) ;
    }
}

请参阅此code run live at IdeOne.com

  

之前:[2019-03-14T12:00,2019-01-17T14:00,2019-02-21T17:00]

     

之后:[2019-01-17T14:00,2019-02-21T17:00,2019-03-14T12:00]


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

相关问题