strtotime()in PHP可以做如下的转换:

投入:

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?

是的,这是一个 重复.但是,原来的问题不回答。我通常需要的能力,以查询的日期。我想给用户能够说'我希望所有事件的从"-1周",以"现在"'.它将使脚本,这些类型的请求,要容易得多。

有帮助吗?

解决方案

我试图实现一个简单的(静态)类模仿的一些模式的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");
Wed Aug 12 22:18:57 CEST 2009
Thu Aug 13 22:18:57 CEST 2009

延伸

例如,让我们加入 天匹配:

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

其他提示

你可以用简单的日期格式为这样的事情,但你必须知道的日期格式之前分析。PHP将试图猜测它,Java希望你告诉他明确要做什么。

例如:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat formater = new SimpleDateFormat("MM/dd/yy");
Date d = parser.parse("2007-04-23 11:22:02");
System.out.println(formater.format(d));

它产出:

04/23/2007

SimpleDateFormat将失败如果不在适当的格式,除非你设置:

parser.setLenient(false);

在这种情况下,它将引发java。的文本。如果.

对于预先格式化使用的日期格式和它的很多 运营商.

看看 JodaTime, 我认为这是最好的datetime库java。

使用一个日历和格式的结果与SimpleDateFormat:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

    Calendar now = Calendar.getInstance();
    Calendar working;
    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

    working = (Calendar) now.clone();

    //strtotime("-2 years")
    working.add(Calendar.DAY_OF_YEAR, - (365 * 2));
    System.out.println("  Two years ago it was: " + formatter.format(working.getTime()));

    working = (Calendar) now.clone();

    //strtotime("+5 days");
    working.add(Calendar.DAY_OF_YEAR, + 5);
    System.out.println("  In five days it will be: " + formatter.format(working.getTime()));

好的,这是明显的更详细的比PHP的strtotime(),但在一天结束,它的功能你之后。

据我所知,没有什么喜欢这个的存在。你会有一个黑客一起你自己。然而,它不可能是必要的。尽量储存的日期,时间戳和只是在做的简单的数学。我明白这不是干净的,你可能会喜欢的。但它的工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top