java - Java 中 PHP 的 strtotime()

标签 java php strtotime

php 中的strtotime() 可以做如下转换:

输入:

strtotime(’2004-02-12T15:19:21+00:00′);
strtotime(’Thu, 21 Dec 2000 16:01:07 +0200′);
strtotime(’Monday, January 1st’);
strtotime(’tomorrow’);
strtotime(’-1 week 2 days 4 hours 2 seconds’);

输出:

2004-02-12 07:02:21
2000-12-21 06:12:07
2009-01-01 12:01:00
2009-02-12 12:02:00
2009-02-06 09:02:41

在java中有没有简单的方法来做到这一点?

是的,这是 duplicate .但是,最初的问题没有得到回答。我通常需要能够查询过去的日期。我想让用户能够说“我想要从“-1 周”到“现在”的所有事件。这将使编写这些类型的请求的脚本变得更加容易。

最佳答案

我尝试实现一个简单的(静态)类来模拟 PHP 的 strtotime 的一些模式。这个类被设计成开放修改(只需通过registerMatcher添加一个新的Matcher):

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");
Wed Aug 12 22:18:57 CEST 2009
Thu Aug 13 22:18:57 CEST 2009

Extending

For example let's add days matcher:

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)

Sat Aug 15 22:18:57 CEST 2009
Sun Aug 09 22:18:57 CEST 2009

关于java - Java 中 PHP 的 strtotime(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1268174/

相关文章:

php - 当前日期 + 2 个月

php - 如何修复 PHP : "A non well formed numeric value encountered" 中的此错误

java - 继承 ParseObject 子类 (Android)

php - 如何使 $_GET 函数与重写的 url 一起工作?

php - symfony 5.1 : how to define a controller as a service

PHP include() 替代方案?

php - strtotime() & date() 将日期转换为与以前相同的格式时出现奇怪的行为

java - Maven部署找不到外部项目依赖

java - 在 MySQL 表中存储多个用户日历

java - 为返回的字符串值设置参数