View Javadoc
1   package cn.home1.oss.lib.common;
2   
3   import static org.joda.time.format.DateTimeFormat.forPattern;
4   
5   import org.joda.time.DateTime;
6   import org.joda.time.DateTimeZone;
7   import org.joda.time.Minutes;
8   import org.joda.time.format.DateTimeFormat;
9   import org.joda.time.format.DateTimeFormatter;
10  
11  import java.nio.charset.Charset;
12  
13  /**
14   * Created by zhanghaolun on 16/6/20.
15   */
16  public abstract class Defaults {
17  
18    private Defaults() {
19    }
20  
21    public static final Charset UTF_8 = Charset.forName("UTF-8");
22  
23    public static final String CRON_EVERY_30_SECONDS = "0/30 * * * * ?";
24  
25    public static final DateTimeZone UTC_P8 = DateTimeZone.forID("Asia/Shanghai");
26  
27    public static final DateTime EPOCH = new DateTime(0L);
28  
29    public static final DateTimeFormatter yyyyMMdd_HHmmss;
30    public static final DateTimeFormatter ISO8601;
31  
32    public static DateTime now() {
33      return DateTime.now(UTC_P8);
34    }
35  
36    public static Integer minutesSinceEpoch(final DateTime dateTime) {
37      return Minutes.minutesBetween(EPOCH, dateTime).getMinutes();
38    }
39  
40    /**
41     * see: http://stackoverflow.com/questions/15245307/java-simpledateformat-timezone-offset-with-minute-separated-by-colon
42     * You can get the timezone offset formatted like +08:00
43     * with the SimpleDateFormat in Java 7 (yyyy-MM-dd'T'HH:mm:ss.SSSXXX)
44     * with the Joda's DateTimeFormat (yyyy-MM-dd'T'HH:mm:ss.SSSZZ)
45     */
46    public static final String PATTERN_JAVA_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
47    public static final String PATTERN_JODA_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
48  
49    static {
50      yyyyMMdd_HHmmss = forPattern("yyyyMMdd-HH:mm:ss").withZone(UTC_P8);
51      ISO8601 = DateTimeFormat.forPattern(PATTERN_JODA_ISO8601).withZone(UTC_P8);
52    }
53  }