Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to implement a simple (static) class that emulates some of the patterns of PHP's <code>strtotime</code>. This class is designed to be <strong>open for modification</strong> (simply add a new <code>Matcher</code> via <code>registerMatcher</code>):</p> <pre><code>public final class strtotime { private static final List&lt;Matcher&gt; matchers; static { matchers = new LinkedList&lt;Matcher&gt;(); matchers.add(new NowMatcher()); matchers.add(new TomorrowMatcher()); matchers.add(new DateFormatMatcher(new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"))); matchers.add(new DateFormatMatcher(new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"))); matchers.add(new DateFormatMatcher(new SimpleDateFormat("yyyy MM dd"))); // add as many format as you want } // not thread-safe public static void registerMatcher(Matcher matcher) { matchers.add(matcher); } public static interface Matcher { public Date tryConvert(String input); } private static class DateFormatMatcher implements Matcher { private final DateFormat dateFormat; public DateFormatMatcher(DateFormat dateFormat) { this.dateFormat = dateFormat; } public Date tryConvert(String input) { try { return dateFormat.parse(input); } catch (ParseException ex) { return null; } } } private static class NowMatcher implements Matcher { private final Pattern now = Pattern.compile("now"); public Date tryConvert(String input) { if (now.matcher(input).matches()) { return new Date(); } else { return null; } } } private static class TomorrowMatcher implements Matcher { private final Pattern tomorrow = Pattern.compile("tomorrow"); public Date tryConvert(String input) { if (tomorrow.matcher(input).matches()) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, +1); return calendar.getTime(); } else { return null; } } } public static Date strtotime(String input) { for (Matcher matcher : matchers) { Date date = matcher.tryConvert(input); if (date != null) { return date; } } return null; } private strtotime() { throw new UnsupportedOperationException(); } } </code></pre> <h2>Usage</h2> <p>Basic usage:</p> <pre><code> Date now = strtotime("now"); Date tomorrow = strtotime("tomorrow"); </code></pre> <pre> Wed Aug 12 22:18:57 CEST 2009 Thu Aug 13 22:18:57 CEST 2009 </pre> <h2>Extending</h2> <p>For example let's add <strong>days matcher</strong>:</p> <pre><code>strtotime.registerMatcher(new Matcher() { private final Pattern days = Pattern.compile("[\\-\\+]?\\d+ days"); public Date tryConvert(String input) { if (days.matcher(input).matches()) { int d = Integer.parseInt(input.split(" ")[0]); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, d); return calendar.getTime(); } return null; } }); </code></pre> <p>then you can write:</p> <pre><code>System.out.println(strtotime("3 days")); System.out.println(strtotime("-3 days")); </code></pre> <p>(now is <code>Wed Aug 12 22:18:57 CEST 2009</code>)</p> <pre> Sat Aug 15 22:18:57 CEST 2009 Sun Aug 09 22:18:57 CEST 2009 </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload