使用Collections.sort()进行日期排序

这篇博客展示了如何使用Java的Collections.sort()方法对日期列表进行排序,并使用SimpleDateFormat进行日期格式化。代码中创建了一个日期列表,然后进行升序排序,最后将排序后的日期列表和一个特定日期格式化后的字符串输出。示例强调了日期处理和集合操作在Java编程中的重要性。
摘要由CSDN通过智能技术生成
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.emory.mathcs.backport.java.util.Collections;

public class CollectionsSort {
	private static final SimpleDateFormat FD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static void main(String[] args) throws ParseException {
		final List<Date> stimes = new ArrayList<Date>();
		Date stime1 = FD.parse("1997-10-29 00:00:00");
		Date stime2 = FD.parse("1997-10-28 00:00:00");
		Date stime3 = FD.parse("1997-10-30 00:00:00");
		stimes.add(stime1);
		stimes.add(stime2);
		stimes.add(stime3);
		System.out.println(stimes.toString());
		Collections.sort(stimes);
		System.out.println(stimes.toString());
		
		String s = FD.format(stime3);
		System.out.println(s);
	}

}

输出:

[Wed Oct 29 00:00:00 CST 1997, Tue Oct 28 00:00:00 CST 1997, Thu Oct 30 00:00:00 CST 1997]
[Tue Oct 28 00:00:00 CST 1997, Wed Oct 29 00:00:00 CST 1997, Thu Oct 30 00:00:00 CST 1997]
1997-10-30 00:00:00

SimpleDateFormat.parse()方法,如果字符串格式不符合,会抛异常,感觉还是不太行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值