String、Date、Calendar之间的转换

原博客地址
Calendar 转化 String

Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
 ```

String 转化Calendar
```java
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
 ```

Date 转化String




"se-preview-section-delimiter">
```java SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); String dateStr=sdf.format(new Date());

String 转化Date

String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);

Date 转化Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());

Calendar转化Date

Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();
 ```

String 转成 Timestamp
```java
Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
 ```

Date 转 TimeStamp




"se-preview-section-delimiter">
```java SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(new Date()); Timestamp ts = Timestamp.valueOf(time);

你可能感兴趣的:(JAVA,java,Date)