java根据时间格式转换为字符串工具类方法总结

1. 根据时间格式转换为字符串(Date date)

public static String getFormatTime(String pattern, Date date) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

return sdf.format(date ==null ?new Date() : date);

}

调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd",new Date());

2.根据时间格式转换为字符串(long date)

/**

* 根据时间格式转换为时间戳

* @param pattern

* @param time

* @return long

*/

public static long getFormatTimeToLong(String pattern, String time) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

try {

if (!TextUtils.isEmpty(time)) {

return sdf.parse(time).getTime();

}

}catch (ParseException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

}

return 0;

}

调用举例:long millisecond = StringUtil.getFormatTimeToLong("yyyy-MM-dd HH:mm:ss", time);

public static String getFormatTime(String pattern,long date) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

Date date2 =new Date();

date2.setTime(date);

return sdf.format(date2);

}

调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd HH:mm:ss", millisecond );

3.根据时间格式转换为字符串(String date)

public static String getFormatTime(String pattern, String date) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

try {

return sdf.format(sdf.parse(date).getTime());

}catch (ParseException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

}

return "";

}

调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd", "String时间字符串");

4.根据格式互转

public static String getFormatTimeFromPatternToPattern(String rawPattern, String pattern, String date) {

SimpleDateFormat sdf =new SimpleDateFormat(rawPattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

SimpleDateFormat newSdf =new SimpleDateFormat(pattern);

try {

return newSdf.format(sdf.parse(date));

}catch (ParseException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

}

return date;

}

举例调用:String strTime=getFormatTimeFromPatternToPattern("yyyy年MM月dd日","yyyy-MM-dd", date)

5.根据时间格式转换为时间戳(返回long类型)

public static long getFormatTimeToLong(String pattern, String time) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

try {

if (!TextUtils.isEmpty(time)) {

return sdf.parse(time).getTime();

}

}catch (ParseException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

}

return 0;

}

举例调用:String strTime=StringUtil.getFormatTime("yyy-MM-dd HH:mm:ss",new Date(StringUtil.getFormatTimeToLong("yyy-MM-dd HH:mm:ss", "String时间字符串")))

6.根据时间格式转换为时间戳(返回Date类型)

public static Date getFormatTimeToDate(String pattern, String time) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

try {

Date newTime = sdf.parse(time);

return newTime;

}catch (ParseException e) {

return new Date();

}

}

举例调用:Date date3 =getFormatTimeToDate("HH:mm", "String时间字符串");

6.根据时间格式转换为字符串(返回String类型的)

public static String getFormatTimeNoZone(String pattern, Date date) {

SimpleDateFormat sdf =new SimpleDateFormat(pattern);

sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

return sdf.format(date ==null ?new Date() : date);

}

举例调用:Calendar calendar = Calendar.getInstance();

Date date2 =new Date(getFormatTimeToLong("yyyy-MM-dd", "String时间字符串"));

calendar.setTime(date2);

calendar.add(Calendar.DATE, +1);

String strTime= StringUtil.getFormatTimeNoZone("yyyy-MM-dd", calendar.getTime());

7.获取中文月份

public static String[]mBigNums =new String[]{"一","二","三","四","五","六","七","八","九","十","十一","十二"};

public static String getBigMonth(int month) {

if (mBigNums.length > month -1) {

return mBigNums[month -1];

}

return "";

}

8.处理时间(毫秒格式 转换为n天前)

public static String fixTime(long timestamp) {

LogUtil.e("时间",getFormatTime("yyyy-MM-dd", timestamp));

try {

if (timestamp ==0) {//不识别

            return "";

}

if (System.currentTimeMillis() - timestamp <1 *60 *1000) {//小于一分钟

            return "刚刚";

}else if (System.currentTimeMillis() - timestamp <60 *60 *1000) {//小于一小时

            return ((System.currentTimeMillis() - timestamp) /1000 /60) +"分钟前";

}else {

Calendar now = Calendar.getInstance();

Calendar c = Calendar.getInstance();

c.setTimeInMillis(timestamp);

if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)

&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)

&& c.get(Calendar.DATE) == now.get(Calendar.DATE)) {//

                return now.get(Calendar.HOUR_OF_DAY) - c.get(Calendar.HOUR_OF_DAY) +"小时前";

}

if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)

&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)

&& c.get(Calendar.DATE) == now.get(Calendar.DATE) -1) {

SimpleDateFormat sdf =new SimpleDateFormat("昨天 HH:mm");

return sdf.format(c.getTime());

}else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)

&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {

return now.get(Calendar.DAY_OF_MONTH) - c.get(Calendar.DAY_OF_MONTH) +"天前";

}else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {

return now.get(Calendar.MONTH) - c.get(Calendar.MONTH) +"月前";

}else {

return new SimpleDateFormat("yyyy年M月d日").format(c.getTime());

}

}

}catch (Exception e) {

e.printStackTrace();

return "";

}

}

调用举例:String strTime=fixTime(StringUtil.getFormatTimeToLong("yyyy-MM-dd HH:mm:ss", freeBean.getConTime()))

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

推荐阅读更多精彩内容