PHP中的PHP strtotime() [英] PHP's strtotime() in Java
问题描述
strtotime()可以进行以下转换:
strtotime() in PHP can do the following transformations:
输入:
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中有一种简单的方法吗?
Is there an easy way to do this in java?
是的,这是重复。但是,最初的问题没有得到回答。我通常需要能够查询过去的日期。我想让用户能够说'我希望所有事件从-1周到现在'。它将使这些类型的请求更容易编写脚本。
Yes, this is a duplicate. However, the original question was not answered. I typically need the ability to query dates from the past. I want to give the user the ability to say 'I want all events from "-1 week" to "now"'. It will make scripting these types of requests much easier.
推荐答案
我试图实现一个模拟一些的简单(静态)类PHP的模式 strtotime
。此类旨在打开以进行修改(只需通过 registerMatcher
添加新的匹配器
):
I tried to implement a simple (static) class that emulates some of the patterns of PHP's strtotime
. This class is designed to be open for modification (simply add a new Matcher
via 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();
}
}
用法
基本用法:
Usage
Basic usage:
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 8月12日22:18:57 CEST 2009
)
Sat Aug 15 22:18:57 CEST 2009
Sun Aug 09 22:18:57 CEST 2009
这篇关于PHP中的PHP strtotime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!