- public class Test(){
- public static String FORMAT_SHORT = "yyyy-MM-dd";
- public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
- public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
- public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
- public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
- public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
- /** 获取时间戳 **/
- public static String getTimeString() {
- SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
- Calendar calendar = Calendar.getInstance();
- return df.format(calendar.getTime());
- }
- /** 获取年份 **/
- public static String getYear(Date date) {
- return format(date).substring(0, 4);
- }
- /** 获取月份 **/
- public static int getMonth(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MONTH) + 1;
- }
- /** 获取日份 **/
- public static int getDay(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.DAY_OF_MONTH);
- }
- /** 获取小时 **/
- public static int getHour(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.HOUR_OF_DAY);
- }
- /** 获取分钟 **/
- public static int getMinute(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MINUTE);
- }
- /** 获取秒钟 **/
- public static int getSecond(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.SECOND);
- }
- /** 获取毫秒 **/
- public static long getMillis(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.getTimeInMillis();
- }
- }