【问题标题】:How to convert the Date data of List<javebean> into SimpleDateFormat如何将 List<javebean> 的日期数据转换为 SimpleDateFormat
【发布时间】:2019-07-19 03:16:55
【问题描述】:

我有一个List,Date数据格式如下:

List = [["Mon Jun 24 09:20:36 CST 2019",1],["Wed Jul 03 14:28:38 CST 2019",2],["Mon Jun 24 17:04:54 CST 2019",1],["Mon Jul 15 16:00:13 CST 2019",1],["Wed Jun 26 11:35:01 CST 2019",1],["Tue Jul 16 10:21:59 CST 2019",0],["Wed Jul 03 14:44:08 CST 2019",1],["Fri Jul 12 17:18:21 CST 2019",1],["Wed Jul 03 15:59:08 CST 2019",1],["Fri Jul 12 15:43:05 CST 2019",0],["Tue Jul 16 14:05:49 CST 2019",1],["Tue Jun 25 10:58:49 CST 2019",1],["Fri Jul 12 15:40:46 CST 2019",0],["Wed Jul 03 15:28:34 CST 2019",1],["Mon Jun 24 15:47:07 CST 2019",0],["Wed Jul 03 16:59:08 CST 2019",1],["Wed Jul 03 17:06:11 CST 2019",1]]

我想把里面的日期改成“yyyy-mm-dd”,然后按日期升序排序,去掉重复的日期。

我使用 JAVA8、Struts2 和 Oracle DB。

我的代码:

List<Event> eventList = eventDao.getAll();

Entity
Table(name = "EVENT")

   NamedQuery(name = "Event.findByStime", query = "SELECT e FROM Event e WHERE e.stime = :stime")

【问题讨论】:

  • 这篇文章即将关闭,请用最少的例子编辑这篇文章,并努力编辑
  • 我建议你不要使用SimpleDateFormat。这个类是出了名的麻烦和过时。而是使用DateTimeFormatterjava.time, the modern Java date and time API 中的其他类。这一点在 Java 8 中更为有效,其中 java.time 是内置的。
  • 使用LocalDate 作为日期。它的toString 方法为您提供了您所要求的yyyy-mm-dd 格式。将您的 bean 放入 TreeMap 并以日期为键,它会为您排序并删除重复项。或者 TreeSet 带有比较日期的比较器。

标签: java simpledateformat dao


【解决方案1】:

//测试pojo类

public class TimeAndInt {

    private Date date;
    private int number;
    private String dateString;

    public TimeAndInt(Date date, int number) {
        this.date = date;
        this.number = number;
    }
}

//测试主类

public class TestMain {


    private static String DateToStringFormat(Date date, String pattern){
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }



    public static void main(String[] args) {
        List<TimeAndInt> list = new ArrayList<TimeAndInt>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,1);
        Date date1 = calendar.getTime();

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        calendar2.add(Calendar.DATE,-2);
        Date date2 = calendar2.getTime();

        list.add(new TimeAndInt(new Date(),2));
        list.add(new TimeAndInt(date1,1));
        list.add(new TimeAndInt(date2,0));

        Collections.sort(list, new Comparator<TimeAndInt>() {
            @Override
            public int compare(TimeAndInt t1, TimeAndInt t2) {
                return t1.getDate().compareTo(t2.getDate());
            }
        });

        for (TimeAndInt timeAndInt : list) {
            timeAndInt.setDateString(DateToStringFormat(timeAndInt.getDate(),"yyyy-MM-dd"));
        }

        System.out.println(list);
    }
}

在应用中使用comparator可以解决问题

【讨论】:

    【解决方案2】:
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
    LocalDateTime d = LocalDateTime.parse("Mon Jun 24 09:20:36 CST 2019", formatter);
    d.format(DateTimeFormatter.ISO_DATE);
    

    【讨论】:

    • 可能有用的代码并演示了现代 Java 日期和时间 API java.time 的使用,谢谢。你需要解释它如何适应提问者的情况,以及它是否以及如何解决他或她的问题。仅代码答案没有帮助。
    猜你喜欢
    • 2021-11-10
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多