Use*_*er1 25 php java 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中有一种简单的方法吗?
是的,这是重复的.但是,最初的问题没有得到回答.我通常需要能够查询过去的日期.我想让用户能够说'我想要所有事件从"-1周"到"现在"'.它将使这些类型的请求更容易编写脚本.
dfa*_*dfa 32
我试图实现一个模拟PHP的一些模式的简单(静态)类strtotime.此类旨在打开以进行修改(只需添加一个新的Matchervia 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();
}
}
Run Code Online (Sandbox Code Playgroud)
基本用法:
Date now = strtotime("now");
Date tomorrow = strtotime("tomorrow");
Run Code Online (Sandbox Code Playgroud)
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;
}
});
Run Code Online (Sandbox Code Playgroud)
然后你可以写:
System.out.println(strtotime("3 days"));
System.out.println(strtotime("-3 days"));
Run Code Online (Sandbox Code Playgroud)
(现在是Wed Aug 12 22:18:57 CEST 2009)
Sat Aug 15 22:18:57 CEST 2009 Sun Aug 09 22:18:57 CEST 2009
| 归档时间: |
|
| 查看次数: |
11050 次 |
| 最近记录: |