Android或者java中的时间日期操作的工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class utils {

   /**
    * 英文简写如:2010
    */
   public static String FORMAT_Y = "yyyy";

   /**
    * 英文简写如:12:01
    */
   public static String FORMAT_HM = "HH:mm";

   /**
    * 英文简写如:1-12 12:01
    */
   public static String FORMAT_MDHM = "MM-dd HH:mm";

   /**
    * 英文简写(默认)如:2010-12-01
    */
   public static String FORMAT_YMD = "yyyy-MM-dd";

   /**
    * 英文全称  如:2010-12-01 23:15
    */
   public static String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";

   /**
    * 英文全称  如:2010-12-01 23:15:06
    */
   public static String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

   /**
    * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
    */
   public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";

   /**
    * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
    */
   public static String FORMAT_FULL_SN = "yyyyMMddHHmmssS";

   /**
    * 中文简写  如:2010年12月01日
    */
   public static String FORMAT_YMD_CN = "yyyy年MM月dd日";

   /**
    * 中文简写  如:2010年12月01日  12时
    */
   public static String FORMAT_YMDH_CN = "yyyy年MM月dd日 HH时";

   /**
    * 中文简写  如:2010年12月01日  12时12分
    */
   public static String FORMAT_YMDHM_CN = "yyyy年MM月dd日 HH时mm分";

   /**
    * 中文全称  如:2010年12月01日  23时15分06秒
    */
   public static String FORMAT_YMDHMS_CN = "yyyy年MM月dd日  HH时mm分ss秒";

   /**
    * 精确到毫秒的完整中文时间
    */
   public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";

   public static Calendar calendar = null;
   private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";


   public static Date str2Date(String str) {
       return str2Date(str, null);
   }


   public static Date str2Date(String str, String format) {
       if (str == null || str.length() == 0) {
           return null;
       }
       if (format == null || format.length() == 0) {
           format = FORMAT;
       }
       Date date = null;
       try {
           SimpleDateFormat sdf = new SimpleDateFormat(format);
           date = sdf.parse(str);
       } catch (Exception e) {
           e.printStackTrace();
       }
       return date;
   }


   public static Calendar str2Calendar(String str) {
       return str2Calendar(str, null);
   }


   public static Calendar str2Calendar(String str, String format) {

       Date date = str2Date(str, format);
       if (date == null) {
           return null;
       }
       Calendar c = Calendar.getInstance();
       c.setTime(date);

       return c;
   }


   public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
       return date2Str(c, null);
   }


   public static String date2Str(Calendar c, String format) {
       if (c == null) {
           return null;
       }
       return date2Str(c.getTime(), format);
   }


   public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
       return date2Str(d, null);
   }


   public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
       if (d == null) {
           return null;
       }
       if (format == null || format.length() == 0) {
           format = FORMAT;
       }
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       String s = sdf.format(d);
       return s;
   }


   public static String getCurDateStr() {
       Calendar c = Calendar.getInstance();
       c.setTime(new Date());
       return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" +
               c.get(Calendar.DAY_OF_MONTH) + "-" +
               c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) +
               ":" + c.get(Calendar.SECOND);
   }


   /**
    * 获得当前日期的字符串格式
    * @param format    格式化的类型
    * @return  返回格式化之后的事件
    */
   public static String getCurDateStr(String format) {
       Calendar c = Calendar.getInstance();
       return date2Str(c, format);

   }


   /**
    *
    * @param time 当前的时间
    * @return  格式到秒
    */
   //
   public static String getMillon(long time) {

       return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

   }


   /**
    *
    * @param time  当前的时间
    * @return  当前的天
    */
   public static String getDay(long time) {

       return new SimpleDateFormat("yyyy-MM-dd").format(time);

   }


   /**
    *
    * @param time 时间
    * @return 返回一个毫秒
    */
   // 格式到毫秒
   public static String getSMillon(long time) {

       return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

   }


   /**
    * 在日期上增加数个整月
    * @param date 日期
    * @param n 要增加的月数
    * @return   增加数个整月
    */
   public static Date addMonth(Date date, int n) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(date);
       cal.add(Calendar.MONTH, n);
       return cal.getTime();

   }


   /**
    * 在日期上增加天数
    * @param date 日期
    * @param n 要增加的天数
    * @return   增加之后的天数
    */
   public static Date addDay(Date date, int n) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(date);
       cal.add(Calendar.DATE, n);
       return cal.getTime();

   }


   /**
    * 获取距现在某一小时的时刻
    *
    * @param format 格式化时间的格式
    * @param h 距现在的小时 例如:h=-1为上一个小时,h=1为下一个小时
    * @return  获取距现在某一小时的时刻
    */
   public static String getNextHour(String format, int h) {
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       Date date = new Date();
       date.setTime(date.getTime() + h * 60 * 60 * 1000);
       return sdf.format(date);

   }


   /**
    * 获取时间戳
    * @return 获取时间戳
    */
   public static String getTimeString() {
       SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
       Calendar calendar = Calendar.getInstance();
       return df.format(calendar.getTime());

   }



   /**
    * 功能描述:返回月
    *
    * @param date Date 日期
    * @return 返回月份
    */
   public static int getMonth(Date date) {
       calendar = Calendar.getInstance();
       calendar.setTime(date);
       return calendar.get(Calendar.MONTH) + 1;
   }


   /**
    * 功能描述:返回日
    *
    * @param date Date 日期
    * @return 返回日份
    */
   public static int getDay(Date date) {
       calendar = Calendar.getInstance();
       calendar.setTime(date);
       return calendar.get(Calendar.DAY_OF_MONTH);
   }


   /**
    * 功能描述:返回小
    *
    * @param date 日期
    * @return 返回小时
    */
   public static int getHour(Date date) {
       calendar = Calendar.getInstance();
       calendar.setTime(date);
       return calendar.get(Calendar.HOUR_OF_DAY);
   }


   /**
    * 功能描述:返回分
    *
    * @param date 日期
    * @return 返回分钟
    */
   public static int getMinute(Date date) {
       calendar = Calendar.getInstance();
       calendar.setTime(date);
       return calendar.get(Calendar.MINUTE);
   }


   /**
    * 获得默认的 date pattern
    * @return  默认的格式
    */
   public static String getDatePattern() {

       return FORMAT_YMDHMS;
   }


   /**
    * 返回秒钟
    *
    * @param date Date 日期
    * @return 返回秒钟
    */
   public static int getSecond(Date date) {
       calendar = Calendar.getInstance();

       calendar.setTime(date);
       return calendar.get(Calendar.SECOND);
   }


   /**
    * 使用预设格式提取字符串日期
    *
    * @param strDate 日期字符串
    * @return 提取字符串的日期
    */
   public static Date parse(String strDate) {
       return parse(strDate, getDatePattern());

   }


   /**
    * 功能描述:返回毫
    *
    * @param date 日期
    * @return 返回毫
    */
   public static long getMillis(Date date) {
       calendar = Calendar.getInstance();
       calendar.setTime(date);
       return calendar.getTimeInMillis();
   }


   /**
    * 按默认格式的字符串距离今天的天数
    *
    * @param date 日期字符串
    * @return 按默认格式的字符串距离今天的天数
    */
   public static int countDays(String date) {
       long t = Calendar.getInstance().getTime().getTime();
       Calendar c = Calendar.getInstance();
       c.setTime(parse(date));
       long t1 = c.getTime().getTime();
       return (int) (t / 1000 - t1 / 1000) / 3600 / 24;

   }


   /**
    * 使用用户格式提取字符串日期
    *
    * @param strDate 日期字符串
    * @param pattern 日期格式
    * @return  提取字符串日期
    */
   public static Date parse(String strDate, String pattern) {
       SimpleDateFormat df = new SimpleDateFormat(pattern);
       try {
           return df.parse(strDate);
       } catch (ParseException e) {
           e.printStackTrace();
           return null;
       }

   }


   /**
    * 按用户格式字符串距离今天的天数
    *
    * @param date 日期字符串
    * @param format 日期格式
    * @return  按用户格式字符串距离今天的天数
    */
   public static int countDays(String date, String format) {
       long t = Calendar.getInstance().getTime().getTime();
       Calendar c = Calendar.getInstance();
       c.setTime(parse(date, format));
       long t1 = c.getTime().getTime();
       return (int) (t / 1000 - t1 / 1000) / 3600 / 24;

   }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容