简体   繁体   English

Java SimpleDateFormat

[英]Java SimpleDateFormat

I have two 2 JTables. 我有两个2 JTables。

Table_1 contains dates that are formatted to ( EE - MMMMM dd, yyyy ) 表_1包含格式为( EE - MMMMM dd, yyyy )的日期

Table_2's values depends on what the user clicked on Table_1. Table_2的值取决于用户在Table_1上单击的内容。 Normally, the date format comming from the database is ( yyyy-dd-MM ) I formatted it to ( EE - MMMMM dd, yyyy ) to look a little bit good. 通常,来自数据库的日期格式为( yyyy-dd-MM )我将其格式化为( EE - MMMMM dd, yyyy )看起来有点不错。

My problem is that, I want to format it back to ( yyyy-dd-MM ) for the query to work. 我的问题是,我想将其格式化回( yyyy-dd-MM )以使查询正常工作。

I want this: 我要这个:

Table_1.getValueAt(Table_1.getSelectedRow(),0) - > Thu - Feb 5, 2015

Back to this: 回到这个:

2015-5-2 

And then query it to the database to bring results. 然后将其查询到数据库以带来结果。

using SimpleDateFormatter and Calendar , 使用SimpleDateFormatterCalendar

String date = "Thu-Feb 5, 2015";

SimpleDateFormat sdf = new SimpleDateFormat("EEE-MMM d, yyyy");

Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));

String newDate = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH);

System.out.println(date);
System.out.println(newDate);

output , 输出

Thu-Feb 5, 2015
2015-5-1

observe that February equals to 1 in above output, this is because a month starts from 0 . 观察到February在上面的输出中等于1 ,这是因为一个月从0开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM