我有一个日期时间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