栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP在Java中的strtotime()

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP在Java中的strtotime()

我试图实现一个简单的(静态)类,该类模仿PHP的某些模式

strtotime
。此类旨在 开放用于修改
(只需
Matcher
通过添加一个新的
registerMatcher
):

public final class strtotime {    private static final List<Matcher> matchers;    static {        matchers = new linkedList<Matcher>();        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();    }}

用法

基本用法:

 Date now = strtotime("now"); Date tomorrow = strtotime("tomorrow");2009年8月12日星期三22:18:57 CEST2009年8月13日星期四22:18:57 CEST

延伸

例如,让我们添加 天数匹配器

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;    }});

然后您可以编写:

System.out.println(strtotime("3 days"));System.out.println(strtotime("-3 days"));

(现在是

Wed Aug 12 22:18:57 CEST 2009

2009年8月15日星期六22:18:57 CEST周日2009年8月9日22:18:57


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568702.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号