java获取当前日期的前一天日期与字符串日期之间的转换
这几天一直在处理Java的日期问题,通常需要按要求生成日期的话,通常会用到util包中的Calendar类,再用Calendar的getTime()方法生成对应的Date,最后用SimpleDateFormat输出。
1. 获取当期日期、年份、月份
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current Year is : " + now.get(Calendar.YEAR));
// month start from 0 to 11
System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1));
System.out.println("Current Date is : " + now.get(Calendar.DATE));
}
}
2. 获取前一天、前一个月的日期
Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
calendar.add(Calendar.DATE, -1); //得到前一天
String yestedayDate
= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
calendar.add(Calendar.MONTH, -1); //得到前一个月
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1; //输出前一月的时候要记得加1
3. 字符串转为日期格式
String date = "2010-02-01 23:59:59";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date d = sf.parse(date);
System.out.println(sf.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
相关文档:
Java一定要掌握的技术
-------------------------------------------------------------------------------
jvm优化
jvm线程调度序列
java gc机制
java gc算法
Java gc原理
java 内存管理
如何主动通知jvm进程垃圾回收
异常机制|java的异常处理
线程|线程的实现
集合类框架
java.io
服务器:Squid、Lighttpd、 ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......