我有一個日期時間picker.the用戶從日期時間選擇器的日期和時間,並在servlet的我用它request.getParameter("");
如何將字符串轉換爲日期在java中
現在如何解析字符串迄今獲得java在插入數據庫時的數據類型爲datetime
。
我有一個日期時間picker.the用戶從日期時間選擇器的日期和時間,並在servlet的我用它request.getParameter("");
如何將字符串轉換爲日期在java中
現在如何解析字符串迄今獲得java在插入數據庫時的數據類型爲datetime
。
June 2009
怎麼樣,因爲你不能說它的日期,你需要通過在month-year
格式中添加一天來使其成爲date
。例如,在這裏添加月份的第一天,並將其設置爲1 June 2009
,然後以期望的格式對其進行解析。
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException, ParseException
{
String dateStr = "28 June 2009";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(new Date(dateStr)));
}
}
當你在mysql中標記這個問題時,所以我可以建議你如何在mysql中做到這一點。您可以通過java傳遞字符串並在插入時進行轉換。
STR_TO_DATE('02-Dec-2011','%d-%M-%Y %h:%m:%s')
這裏是在SimpleDateFormat的
String string = "2014-04-21 17:35:00";
Calendar calender = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
Date date;
date = format.parse(string);
calender.setTime(date);
System.out.println("Day of week: " + calender.get(Calendar.DAY_OF_WEEK)); //2 is monday
System.out.println("Day of the month: " + calender.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: " + calender.get(Calendar.MONTH)); //0 is january 3 is april
System.out.println("Year: " + calender.get(Calendar.YEAR));
System.out.println("Hour: " + calender.get(Calendar.HOUR_OF_DAY));
System.out.println("Minutes: " + calender.get(Calendar.MINUTE));
解析日期例如一個例子:
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
Date d = null;
d = parser.parse("String that is formatted as the above date format");
你真的應該已經能夠找到你自己,有這麼多例如,如果你谷歌它。
請閱讀'SimpleDateFormat'類,特別是'parse'方法。 –
你有沒有試過[谷歌搜索你的問題標題?](https://www.google.com/#q=How+to+convert+a+string+to+date+in+java) – Baby