strtotime() in PHP can do the following transformations:
Inputs:
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’);
Outputs:
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
Is there an easy way to do this in java?
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.
The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
Code for converting a string to dateTime $input = '06/10/2011 19:00:02' ; $date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );
Answer: Use the strtotime() Function You can use the PHP strtotime() function to convert any textual datetime into Unix timestamp.
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(); } }
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
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; } });
then you can write:
System.out.println(strtotime("3 days")); System.out.println(strtotime("-3 days"));
(now is Wed Aug 12 22:18:57 CEST 2009
)
Sat Aug 15 22:18:57 CEST 2009 Sun Aug 09 22:18:57 CEST 2009
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With