1.string格式转化为Date对象:
//把string转化为date
DateFormat fmt =new
SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(szBeginTime);
test.setStartTime(date);
注意:引入的是:java.text.DateFormat
2.Date格式转化为String对象:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String startTime = sdf.format(startTime);
Java时间格式转换大全
import java.text.*;
import java.util.Calendar;
public class VeDate {
public static Date getNowDate() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
ParsePosition pos = new
ParsePosition(8);
Date currentTime_2 =
formatter.parse(dateString, pos);
return currentTime_2;
}
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = null;
String str = null;
// String转Date
str = "2007-1-18";
try {
date =
format1.parse(str);
data =
format2.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
public static String getStringDate() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
return dateString;
}
public static String getStringDateShort() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd");
String dateString =
formatter.format(currentTime);
return dateString;
}
public static String getTimeShort() {
SimpleDateFormat formatter =
new SimpleDateFormat("HH:mm:ss");
Date currentTime = new
Date();
String dateString =
formatter.format(currentTime);
return dateString;
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new
ParsePosition(0);
Date strtodate =
formatter.parse(strDate, pos);
return strtodate;
}
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(dateDate);
return dateString;
}
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd");
String dateString =
formatter.format(dateDate);
return dateString;
}
public static Date strToDate(String strDate) {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new
ParsePosition(0);
Date strtodate =
formatter.parse(strDate, pos);
return strtodate;
}
public static Date getNow() {
Date currentTime = new
Date();
return currentTime;
}
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm =
date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new
Date(date_3_hm);
return date_3_hm_date;
}
public static String getStringToday() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString =
formatter.format(currentTime);
return dateString;
}
public static String getHour() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
String hour;
hour =
dateString.substring(11, 13);
return hour;
}
public static String getTime() {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
String min;
min = dateString.substring(14,
16);
return min;
}
public static String getUserDate(String sformat) {
Date currentTime = new
Date();
SimpleDateFormat formatter =
new SimpleDateFormat(sformat);
String dateString =
formatter.format(currentTime);
return dateString;
}--------------------------------------------------------------------------------------------------------------------------------
做成方法
import java.util.*;
import java.text.*;
import java.util.Calendar;
public class VeDate {
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString,
pos);
return currentTime_2;
}
public static Date getNowDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd");
String dateString =
formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString,
pos);
return currentTime_2;
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(currentTime);
return dateString;
}
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd");
String dateString =
formatter.format(currentTime);
return dateString;
}
public static String getTimeShort() {
SimpleDateFormat formatter = new
SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString =
formatter.format(currentTime);
return dateString;
}
public static Date strToDateLong(String strDate)
{
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate,
pos);
return strtodate;
}
public static String dateToStrLong(java.util.Date
dateDate) {
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString =
formatter.format(dateDate);
return dateString;
}
public static String dateToStr(java.util.Date
dateDate) {
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd");
String dateString =
formatter.format(dateDate);
return dateString;
}
public static Date strToDate(String strDate)
{
SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate,
pos);
return strtodate;
}