tl;博士
永远不要使用糟糕的旧日期时间类,例如 Date 和 Calendar。
始终使用它们的替代品,即 JSR 310 中定义的现代 java.time 类。大部分功能已在 ThreeTen-Backport中向后移植到 Java 6 和 7 >.
List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );
List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
for ( String input : inputs )
{
try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
}
Collections.sort( yearMonths , Collections.reverseOrder() );
将 java.time 向后移植到 Java 7
您说您仅限于 Java 7。虽然不是内置的,但您可以使用大多数现代 java.time 功能。将ThreeTen-Backport 库添加到您的项目中。
如果使用 Maven,请将其添加到您的 POM:
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.5.1</version>
</dependency>
Date、Calendar、SimpleDateFormat 等传统日期时间类在设计上存在严重缺陷。它们真的是一团糟,以至于在你的项目中添加一个库是非常值得的。
DateTimeFormatter
设置您的输入。
顺便说一句,您的输入文本使用的是自定义格式 M-YYYY。我建议您对数据的发布者进行有关ISO 8601 标准的教育,以格式化以文本方式交换的日期时间值。年月的标准格式是 YYYY-MM。
定义格式化程序以匹配您的非标准输入。
List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );
YearMonth
解析您的输入 YearMonth 对象,为每个对象添加一个新的 List。
如果输入可能有问题,您可以捕获DateTimeException 来检测和处理此类问题。
List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
for ( String input : inputs )
{
try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
}
Collections.sort && Collections.reverseOrder
对您的列表进行排序。您想要最新的第一个,因此传递可选的第二个参数 Comparator 以反转自然顺序的排序。
Collections.sort( yearMonths , Collections.reverseOrder() );
报告我们的新名单。
System.out.println( "yearMonths = " + yearMonths );
示例代码
将所有代码放在一起。
package work.basil;
import org.threeten.bp.DateTimeException;
import org.threeten.bp.YearMonth;
import org.threeten.bp.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Example parsing year-month values using ThreeTen-Backport library
* giving us most of the Java 8+ *java.time* functionality.
**/
public class App
{
public static void main ( String[] args )
{
List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );
List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
for ( String input : inputs )
{
try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
}
Collections.sort( yearMonths , Collections.reverseOrder() );
System.out.println( "yearMonths = " + yearMonths );
}
}
运行时。
年月 = [2014-12, 2014-05, 2012-11, 2012-08]
顺便说一下,这里是这个应用程序的一个 Maven POM,使用了各个部分的最新版本。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil</groupId>
<artifactId>J7</artifactId>
<version>1.0-SNAPSHOT</version>
<name>J7</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--https://www.threeten.org/threetenbp/-->
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>