我有两个2号的JTables。
Table_1包含的日期格式为(EE - MMMMM dd, yyyy)
Table_2的值取决于用户在Table_1上点击了什么。通常,从数据库开始的日期格式是(yyyy-dd-MM),我将其格式化为(EE - MMMMM dd, yyyy)以使其看起来更好一些。
我的问题是,我希望将其格式化为(yyyy-dd-MM),以便查询正常工作。
我想要的是:
Table_1.getValueAt(Table_1.getSelectedRow(),0) - > Thu - Feb 5, 2015回到这里:
2015-5-2 然后将其查询到数据库以带来结果。
发布于 2015-02-05 23:34:00
使用SimpleDateFormatter和Calendar,
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);输出,
Thu-Feb 5, 2015
2015-5-1注意到在上面的输出中February等于1,这是因为一个月从0开始。
https://stackoverflow.com/questions/28347451
复制相似问题