日期解析从一种格式转换为另一种格式

编程入门 行业动态 更新时间:2024-10-09 16:30:58
本文介绍了日期解析从一种格式转换为另一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将日期格式yyyy-mm-dd hh:mm:ss.SSS(以字符串格式存储在数据库中)更改为mm/dd/yyyy进行比较

I want to change date format yyyy-mm-dd hh:mm:ss.SSS ( which is stored in database in string format) to mm/dd/yyyy for their comparison

while(rs.next()) { reportBean bean=new reportBean(); String proj_close_date=rs.getString(3); String added_on=rs.getString(4); DateFormat myDateFormat = new SimpleDateFormat("MM/dd/yyyy"); DateFormat myDateFormat1= new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSSSSS"); Date myDate1 = null; Date myDate2 = null; Date myDate3 = null; Date myDate4 = null; Date myDate5 = null; try { if(proj_close_date==null || proj_close_date.trim().equals("")) { System.out.println("\n ****** In IF Loop "); bean.setCust_code(rs.getString("customer_code")); bean.setProject_code(rs.getString("project_code")); list.add(bean); } else { System.out.println("\n ****** In Else Loop "); myDate1 = myDateFormat.parse(proj_close_date); myDate2 = myDateFormat.parse(frm_date); myDate3 = myDateFormat.parse(to_date); myDate5 = myDateFormat1.parse(added_on); myDate4 = myDateFormat.format(myDate5); System.out.println("Project Code ---->"+rs.getString(2)); System.out.println("Proj_close_date ------>"+myDate1); System.out.println("From Date ---->"+myDate2); System.out.println("to Date ---->"+myDate3); System.out.println("Added_on --->"+myDate4); System.out.println("Added_on 1 ie Date 5 ---->"+myDate5); if(myDate1.after(myDate2) && myDate1.before(myDate3)) // means --> if(proj_close_date.after(frm_date) && proj_close_date.before(to_date)) { if(myDate1.after(myDate4)) // means --> if(proj_close_date.after(added_on)) { bean.setCust_code(rs.getString("customer_code")); bean.setProject_code(rs.getString("project_code")); list.add(bean); } else { bean.setCust_code(rs.getString("customer_code")); bean.setProject_code(rs.getString("project_code")); list.add(bean); } }//if }//else }//try catch (ParseException e) { System.out.println("Invalid Date Parser Exception "); e.printStackTrace(); } } rs.close(); stmt.close(); } catch(SQLException sex) { sex.printStackTrace(); } finally { closeConnection(); }

推荐答案

一些注意事项

  • 约定用于Java类名,每个都有大写的名词, reportBean 变为 ReportBean
  • 不要按位置引用SQL列,请始终使用名称代替 rs.getString("customer_code")而不是 rs.getString(3)
  • 使用有意义的变量名, myDate1 变为 closeDate
  • 实践调试代码,这样就可以消除 System.out.println()
  • 优美地释放资源, stmt.close()在finally块内移动
  • 使用记录框架,而不是吞下 Exception ,例如 log.error(无效的日期解析器异常",e);
  • convention is for Java class names to have each noun capitalised, reportBean becomes ReportBean
  • don't refer to SQL columns by position, always use a name instead rs.getString("customer_code") rather than rs.getString(3)
  • use meaningful variable names, myDate1 becomes closeDate
  • practice debugging your code so you can eliminate System.out.println()
  • gracefully release resources, stmt.close() moves within a finally block
  • use a logging framework, rather than swallowing Exception, e.g. log.error("Invalid Date Parser Exception", e);

一些特定的指针:

new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSSSSS") // as already noted, mm is the format for minute, MM is the format for month

myDate4 = myDateFormat.format(myDate5); // invalid as you are asigning a String to a Date

if(myDate1.after(myDate4)) // irrelevant as both if & else block execute the same code

rs.close() // not necessary as closed when `Statement` is closed

请参见 Javadoc

您确定数据库架构是所有 varchar 列吗?我建议您修复这种情况.否则,您可以调用 rs.getDate() .

Are you sure that your database schema is all varchar columns? I'd recommend that you fixed that if its the case. Otherwise you can call rs.getDate() instead.

更多推荐

日期解析从一种格式转换为另一种格式

本文发布于:2023-05-25 19:52:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   转换为   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!