Android 字符串转指定格式时间 日期处理 去掉月份或天多余的0 获取今天 昨天 明天等的日期和星期
2015-01-16 10:30
1311 查看
都是些基础知识,不过偶java学的不咋样,记录下吧。
字符串转化成指定格式时间:
主要是格式化的时候指定。
格式化成 ("M-d") 形式,日期即不会再有多余的0, MM dd会自动填充 0, M d则不会。
M -- 月份
m -- 分钟
得到指定的日期:
得到指定的星期:
运行结果:
字符串转化成指定格式时间:
//字符串转指定格式时间 public static String getMyDate(String str) { return StringToDate(str, "yyyy-MM-dd", "yyyy/MM/dd"); } public static String StringToDate(String dateStr, String dateFormatStr, String formatStr) { DateFormat sdf = new SimpleDateFormat(dateFormatStr); Date date = null; try{ date = sdf.parse(dateStr); } catch (ParseException e){ e.printStackTrace(); } SimpleDateFormat s = new SimpleDateFormat(formatStr); return s.format(date); }
主要是格式化的时候指定。
格式化成 ("M-d") 形式,日期即不会再有多余的0, MM dd会自动填充 0, M d则不会。
M -- 月份
m -- 分钟
得到指定的日期:
/** * 取得今天,昨天,前天,明天,后天...的日期 * @param sel --- 0->当天 -1->昨天 -2->前天 1->明天 2->后天 ...... * @return ------- 返回指定日期 */ public static String getOurSelData(int sel){ String str = ""; //格式化日期格式 SimpleDateFormat df = new SimpleDateFormat("M" + "月" + "d" + "日"); Calendar calendar = Calendar.getInstance(); calendar.roll(Calendar.DAY_OF_YEAR, sel); str = df.format(calendar.getTime()); return str; }
得到指定的星期:
/** * 取得今天,昨天,前天,明天,后天....的星期 * @param sel --- 0->当天 -1->昨天 -2->前天 1->明天 2->后天 ...... * @return ------ 返回指定的星期 */ public static String getOurSelWeek(int sel){ Date date = null; String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; SimpleDateFormat df = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); calendar.roll(Calendar.DAY_OF_YEAR, sel); String str = df.format(calendar.getTime()); try{ date = df.parse(str); } catch (ParseException e){ e.printStackTrace(); } calendar.setTime(date); int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; return weekDaysName[intWeek]; }
运行结果:
相关文章推荐
- 批处理生成指定格式的日期时间字符串
- Android 开发之 SimpleDateFormat 类解析&输出指定格式的日期时间字符串
- linux中获取昨天、今天、明天的日期(时间)
- mysql 获取昨天日期、今天日期、明天日期以及前一个小时和后一个小时的时间
- java获取各种格式的时间,获取昨天明天日期,获取一天的开始结束时间
- mysql 获取昨天日期、今天日期、明天日期以及前一个小时和后一个小时的时间
- scala时间处理-获取今天日期,昨天日期,本周时间,本月时间,时间戳转换日期,时间比较
- android 日期格式处理 ,获取当前系统时间并设置格式为--年--月--日
- java获取各种格式的时间,获取昨天明天日期,获取一天的开始结束时间
- Android获取昨天今天明天后天代码实现。以及月份前去掉前缀0
- js中使用eval()方法将字符串转换成日期格式、并获取指定时间的日期
- Java时间戳转化为今天、昨天、明天(字符串格式)
- 时间戳转化为今天、昨天、明天(字符串格式)
- Java时间戳转化为今天、昨天、明天(字符串格式)
- mysql 获取昨天日期、今天日期、明天日期以及前一个小时和后一个小时的时间
- js 获取前天、昨天、今天、明天、后天的时间 (转)
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- php 获取时间今天明天昨天时间戳
- java获取日期及获取昨天、今天和明天的日期
- PHP获取今天、昨天、明天的日期