仅比较日期的某些部分

时间:2015-01-27 10:33:18

标签: java

我想知道我是否有以下数组

A = [JAN-2014, FEB-2014, MARCH-2014]
B = [1-JAN-2014, 13-FEB-2014, 29-MARCH-2014]
C = [2014-Jan, 2014-Feb, 2014-March]

是否可以仅基于月份比较3个阵列之间的日期而不转换任何一个数组的日期格式以跟随其他日期?

2 个答案:

答案 0 :(得分:1)

我建议使用

    public static final SimpleDateFormat SDF_DATE = new SimpleDateFormat("yyyy MMM dd");
    public static final SimpleDateFormat SDF_HOUR = new SimpleDateFormat("HH:mm:ss");

    public String getDateAddedToString() {
            return DateFormatters.SDF_DATE.format(dateAdded.getTime());
    }

..然后..

"example_string".equals(getDateAddedToString());

使用数月:

public static final SimpleDateFormat SDF_MONTH = new SimpleDateFormat("MMM")

或者使用String类的substringregex

问候!

答案 1 :(得分:0)

String aString = "JAN-2014";
SimpleDateFormat aDateFormat = new SimpleDateFormat("mmm-yyyy");
Date aDate = aDateFormat.parse(aString);

String bString = "13-FEB-2014";
SimpleDateFormat bDateFormat = new SimpleDateFormat("dd-mmm-yyyy");
Date bDate = bDateFormat.parse(bString);

String cString = "2014-feb";
SimpleDateFormat cDateFormat = new SimpleDateFormat("yyyy-mmm");
Date cDate = cDateFormat.parse(cString);

Calendar aCal = Calendar.getInstance();
aCal .setTime(aDate);

Calendar bCal = Calendar.getInstance();
bCal .setTime(bDate);

Calendar cCal = Calendar.getInstance();
cCal .setTime(cDate);

int aMonth = aCal.get(Calendar.MONTH);
int bMonth = bCal.get(Calendar.MONTH);
int cMonth = cCal.get(Calendar.MONTH);

然后你可以比较月份的整数值