Date Parsing from one format to another format

后端 未结 2 448
[愿得一人]
[愿得一人] 2021-01-25 08:22

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()) 
           


        
相关标签:
2条回答
  • 2021-01-25 08:43

    A few notes

    • 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);

    Some specific pointers:

    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
    

    see Javadoc

    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.

    0 讨论(0)
  • 2021-01-25 08:52

    You have set myDateFormat1 to "yyyy-mm-dd hh:mm:ss.SSSSSS". I think the first mm should be in uppercase.

    I recommend you check your format strings with the documentation if SimpleDateFormat.

    0 讨论(0)
提交回复
热议问题