【Java】【9】String Date Calendar之间的转换
前言:
1, Calendar 转化 String
2, Calendar 转化 Date
3,Date 转化 String
4,Date 转化 Calendar
5,String 转化 Calendar
6,String 转化 Date
7,Date 转化 TimeStamp
8,String 转化 TimeStamp
正文:
1, Calendar 转化 String
Calendar calendat = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(calendar.getTime());
2, Calendar 转化 Date
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
3,Date 转化 String
Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(date);
4,Date 转化 Calendar
Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);
5,String 转化 Calendar
String str = "2012-5-27"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(str); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);
6,String 转化 Date
String str = "2012-5-27"; SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date= sdf.parse(str);
7,Date 转化 TimeStamp
Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(date); Timestamp ts = Timestamp.valueOf(time);
8,String 转化 TimeStamp
Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
参考博客:
1,[java]转:String Date Calendar之间的转换 - 祺祺怪怪的 - 博客园
https://www.cnblogs.com/qima/p/3652566.html