Java Regex Date Vaidate dateToString(Date date)

Here you can find the source of dateToString(Date date)

Description

Return a String suitable for use as an edn #inst , given a Date .

License

Open Source License

Return

an RFC3339 compatible string.

Declaration

public static String dateToString(Date date) 

Method Source Code

//package com.java2s;
// (c) 2012 B Smith-Mannschott -- Distributed under the Eclipse Public License

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import java.util.regex.Pattern;

public class Main {
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");

    /**//from   ww w.  j  a  v a  2 s  .co m
     * Return a String suitable for use as an edn {@code #inst}, given
     * a {@link Date}.
     * @return an RFC3339 compatible string.
     */
    public static String dateToString(Date date) {
        GregorianCalendar c = new GregorianCalendar(GMT);
        c.setTime(date);
        String s = calendarToString(c);
        assert s.endsWith("+00:00");
        return s.substring(0, s.length() - 6) + "-00:00";
    }

    /**
     * Return a String suitable for use as an edn {@code #inst}, given
     * a {@link GregorianCalendar}.
     * @return an RFC3339 compatible string.
     */
    public static String calendarToString(GregorianCalendar cal) {
        String s = String.format("%1$tFT%1$tT.%1$tL%1$tz", cal);
        /* s is almost right, but is missing the colon in the offset */
        assert Pattern.matches(".*[-+][0-9]{4}$", s);
        int n = s.length();
        return s.substring(0, n - 2) + ":" + s.substring(n - 2);
    }
}

Related

  1. dateMatcher(String dt)
  2. dateOfAny(final String value)
  3. datePatternPattern(String appenderPrefix)
  4. dateTimeMatcher(String dt)
  5. dateTimePattern()
  6. parseDate(final String date, final DateTimeFormatter parser)
  7. parseDate(String date)
  8. parseDate(String dateStr)
  9. parseDate(String entry)