package com.java.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateStringTransfrom {
public static void main(String[] args) {
stringTransfromDateOne();
//stringTransformDateTwo();
//DateTransformStringOne();
//DateTransformStringTwo();
}
// yyyy-MM-dd HH:mm:ss格式的字符串转换为Date对象
static void stringTransfromDateOne() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = dateFormat.parse("2010-10-01 12:00:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("date object: " + date);
}
// yyyy-MM-dd格式的字符串转换为Date对象
static void stringTransformDateTwo() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dateFormat.parse("2010-10-01");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("date object: " + date);
}
// 当前时间对象转换为字符串
static void DateTransformStringOne() {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = dateFormat.format(date);
System.out.println("dateString: " + dateString);
}
//指定日期对象转换为字符串
static void DateTransformStringTwo() {
// 创建指定日期对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = dateFormat.parse("2010-10-01 12:00:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dateString = dateFormat.format(date);
System.out.println("dateString: " + dateString);
}
}