java中字符串转换为日期并且格式化
2016-02-18 08:46
489 查看
/**
* 字符串转换为日期
* 2016-2-18
* @param dateString 要转换的字符串 20150102
* @param dateFormat 要转换成的字符串格式 yyyyMMdd
* @return
*/
private static Date StrToDate(String dateString,String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = null;
try {
date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取为下个月1号的时间
* @return
*/
private static String nextMonth(){
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(calendar.MONTH, +1); //设置为前3月
dBefore = calendar.getTime(); //得到前3月的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
String nextMon = defaultStartDate.substring(0,7)+"-01";
return nextMon;
}
* 字符串转换为日期
* 2016-2-18
* @param dateString 要转换的字符串 20150102
* @param dateFormat 要转换成的字符串格式 yyyyMMdd
* @return
*/
private static Date StrToDate(String dateString,String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = null;
try {
date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取为下个月1号的时间
* @return
*/
private static String nextMonth(){
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(calendar.MONTH, +1); //设置为前3月
dBefore = calendar.getTime(); //得到前3月的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
String nextMon = defaultStartDate.substring(0,7)+"-01";
return nextMon;
}
相关文章推荐
- The Java™ Tutorials — Concurrency :Guarded Blocks 保护块
- myeclipse设置凝视
- java static关键字
- Java中带条件的查询
- Java中字符串相等与大小比较
- DBManager类(java jdbc数据库连接及操作)
- Atitit swt 4.3 4.4 4.5 新特性java attilax总结
- Atitit swt 4.3 4.4 4.5 新特性java attilax总结
- Atitit swt 4.3 4.4 4.5 新特性java attilax总结
- java并发编程——线程异常处理\资源共享的问题\ThreadLocal
- sublime 编译java
- Java内部类
- java.lang.NullPointerException: Expected timestamp
- Mybatis Could not find result map java.lang.Intege
- java 通过Spark实现单词计数的功能
- SpringMVC 使用Fastjson代替Jackson
- 在web应用中获取spring的context,并打印出context中所有bean的id
- Spring+SpringMVC+MyBatis框架搭建
- jdk_自带的 logUtils日志记录工具
- java md5加密 简单加密