Java使用不同的格式从TextField中解析日期 [英] Java parse date from TextField with a different format

查看:304
本文介绍了Java使用不同的格式从TextField中解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse中我遇到了一些日期问题。我在TextField中显示日期格式为
$ b $ p $ dd $ MM

,我需要从这个文本字段中获取文本,并将其解析为一个日期,将它插入到我的数据库中的一个类型为 DATE 我正在使用MySQL,它接受日期为

yyyy-MM-dd



我尝试了很多选项,都使用 LocalDate 或更旧的 Date ,但没有找到解决办法。有人可以帮忙吗?谢谢!

解决方案

之前的Java 8方法



从你的 TextField 检索你的字符串,你应该把它解析为 java.util.Date

  String text = textField.getText(); 
SimpleDateFormat sdf = new SimpleDateFormat(dd / MM / yyyy);
java.util.Date textFieldAsDate = null;

尝试{
textFieldAsDate = sdf.parse(text);
} catch(ParseException pe){
//处理ParseException
}

之后,可以将 java.util.Date 转换为 java.sql.Date ,将它存储到你的MySQL数据库中,如下所示:

  sdf = new SimpleDateFormat(yyyy-MM-dd); 
java.sql.Date date = java.sql.Date.valueOf(sdf.format(textFieldAsDate));



Java 8方式



@BasilBourque,Java日期和时间机制由java.util.Date,java.util.Calendar和java.util.TimeZone类提供,它们现在是 legacy

因此,为了完成使用Java 8,前面提到过,可以这样做:

  String text = textField.getText(); 
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(dd / MM / yyyy);
java.time.LocalDate textFieldAsDate = java.time.LocalDate.parse(text,formatter);

之后,为了将 LocalDate java.sql.Date 中,将其存储到MySQL数据库中,可以执行以下操作:

  java.sql.Date sqlDate = java.sql.Date.valueOf(textFieldAsDate); 


I'm facing some problems in Eclipse with dates. I'm showing a date in a TextField with the format

dd/MM/yyyy

and I need to get the text from this textfield and parse it as a date to insert it in my database into a column of type DATE I'm using MySQL and it accepts dates as

yyyy-MM-dd

I tried really many options, both with LocalDate or the older Date, but didn't find a solution. Can someone please help? Thank you!

解决方案

Prior Java 8 approach

Firstly, after retrieving your string from your TextField, you should parse it to java.util.Date:

String text = textField.getText();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date textFieldAsDate = null;

try {
    textFieldAsDate = sdf.parse(text);
} catch (ParseException pe) {
    // deal with ParseException
}

Afterwards, you can convert your java.util.Date into a java.sql.Date, to store it into your MySQL database, like this:

sdf = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date date = java.sql.Date.valueOf(sdf.format(textFieldAsDate));

Java 8 approach

As mentioned by @BasilBourque, the Java date and time mechanism was provided by java.util.Date, java.util.Calendar, and java.util.TimeZone classes which are now legacy.

Therefore, in order to accomplish using Java 8 what was previously mentioned, one can do the following:

String text = textField.getText();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("dd/MM/yyyy");
java.time.LocalDate textFieldAsDate = java.time.LocalDate.parse(text, formatter);

Afterwards, in order to convert the LocalDate into a java.sql.Date, to store it into your MySQL database, it's possible to do the following:

java.sql.Date sqlDate = java.sql.Date.valueOf(textFieldAsDate);

这篇关于Java使用不同的格式从TextField中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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