将字符串dd/MM/yyyy日期转换为java.sql.date yyyy-MM-dd [英] Convert String dd/MM/yyyy date into java.sql.date yyyy-MM-dd

查看:154
本文介绍了将字符串dd/MM/yyyy日期转换为java.sql.date yyyy-MM-dd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法进行上述转换.但是不推荐使用java.sql.Date方法.那么,使用这种方法对他们有没有害处?由于下面的语句工作正常.

I am using the below method to do the described conversion. But the java.sql.Date method is deprecated. So, is their any harm in using this method? As the statements below are working fine.

String f33_ = (String) session.getAttribute("sf33");


java.sql.Date f33 = new java.sql.Date(Integer.parseInt(f33_.substring(6, 10))- 1900, Integer.parseInt(f33_.substring(3, 5))-1,Integer.parseInt(f33_.substring(0, 2))); 

推荐答案

您应使用SimpleDateFormat(或 Joda Time ),而不是尝试自己解析:

You should use SimpleDateFormat (or the equivalent from Joda Time) rather than trying to parse this yourself:

DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

java.util.Date date = format.parse(text);

java.sql.Date sqlDate = new java.sql.Date(date.getTime());

这篇关于将字符串dd/MM/yyyy日期转换为java.sql.date yyyy-MM-dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆