开发者

Debug simple java code related to Calendar Date GMT

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Test {

    public static void main(String[] args) throws ParseException {

        Calendar dateFromNet = strToCal("11-MAR-2004", "dd-MMM-yyyy");
        Calendar IEndTime = strToCal("20-05-2004", "dd-mm-yyyy");

        if (dateFromNet.after(IEndTime) ) {

            System.out.println(dateFromNet);
            System.out.println(IEndTime);

            System.out.println("not true: 11-MAR-2004(11-3-2004) > 20-05-2004 ");
        }


    }

    private static Calendar strToCal(String date, String format) throws ParseException {
        SimpleDateFormat input = new SimpleDateFormat(format);
        input.setTimeZone(TimeZone.getTimeZone("GMT"));

    开发者_如何学运维    Date d = (Date) input.parse(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c;
    }
}

This test shows

dateFromNet.after(IEndTime) == true

i.e. 11-03-2004 is after 20-05-2004

What have I done wrong?


Calendar IEndTime = strToCal("20-05-2004", "dd-mm-yyyy");

mm is for milliseconds; make those capitol M, like this:

Calendar IEndTime = strToCal("20-05-2004", "dd-MM-yyyy");


The letter, m and M have different meanings as shown in the following table:

Letter Date or Time Component Presentation Examples
m Minute in hour Number 30
M Month in year Month July; Jul; 07

So, the root cause of the problem is using m instead of M in the pattern, dd-mm-yyyy.

java.time

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Demo of the modern API:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {

        ZonedDateTime dateFromNet = strToZdt("11-MAR-2004", "d-MMM-u");
        ZonedDateTime IEndTime = strToZdt("20-05-2004", "d-M-u");

        if (dateFromNet.isAfter(IEndTime)) {
            System.out.println("11-MAR-2004 > 20-05-2004");
        } else if (dateFromNet.isBefore(IEndTime)) {
            System.out.println("11-MAR-2004 < 20-05-2004");
        } else {
            System.out.println("11-MAR-2004 = 20-05-2004");
        }
    }

    private static ZonedDateTime strToZdt(String date, String format) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .appendPattern(format)
                                .toFormatter(Locale.ENGLISH);

        LocalDate localDate = LocalDate.parse(date, dtf);

        return localDate.atStartOfDay(ZoneId.of("Etc/UTC"));
    }
}

Output:

11-MAR-2004 < 20-05-2004

If at all you need an object of java.util.Calendar from this object of ZonedDateTime, you can do so as follows:

Calendar calendar = Calendar.getInstance();
calendar.setTime(Date.from(dateFromNet.toInstant()));

Learn more about the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜