[英]How to convert a date format to different date format in java?
i want to convert a date format to another format: 我想将日期格式转换为另一种格式:
try {
System.out.println("try block tai asche naki ?" + stockData.getTRANSDATE());
SimpleDateFormat inFmt = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = inFmt.parse(stockData.getTRANSDATE().toString());
System.out.println("conversion" +inFmt.parse(stockData.getTRANSDATE().toString()) );
System.out.println("old date format ?" + date);
//date1 = outFmt.format(date);
System.out.println("new date format "+ outFmt.format(date));
return template
.queryForInt(
"SELECT COUNT(TRANSDATE) FROM stockdata
WHERE TRANSDATE= ? AND symbolname = ?",
rowMapper,outFmt.format(date), SymbolName);
}
as i want to use it in a sql query its not working please help me out how i will convert the date so i can use it in the db as outFmt.format(date) cant be resolved in DB query 因为我想在SQL查询中使用它不起作用,请帮我解决一下如何转换日期,以便我可以在数据库中使用它,因为outFmt.format(date)在数据库查询中无法解决
Thanks 谢谢
Dateformat is for parsing string to java Date type or output date object to text with certain pattern. Dateformat用于将字符串解析为java Date类型或将date对象输出为具有特定模式的文本。
In your case, you need a Date object for your query, not a String. 在您的情况下,您需要一个Date对象而不是String来进行查询。
is this code working for you? 该代码对您有用吗?
return template
.queryForInt(
"SELECT COUNT(TRANSDATE) FROM stockdata WHERE TRANSDATE= ? AND symbolname = ?",
rowMapper,date, SymbolName);
I'm guessing that you're using Spring's JdbcTemplate. 我猜您正在使用Spring的JdbcTemplate。 Why don't you just bind a
java.sql.Date
or java.sql.Timestamp
instead of formatting it? 为什么不只绑定
java.sql.Date
或java.sql.Timestamp
而不格式化它?
UPDATE : 更新 :
I don't know what stockData.getTRANSDATE()
is, but in order to convert it to Timestamp
, do this: 我不知道
stockData.getTRANSDATE()
是什么,但是为了将其转换为Timestamp
,请执行以下操作:
Timestamp date = new TimeStamp(
inFmt.parse(stockData.getTRANSDATE().toString()).getTime());
Then, pass it to Spring as a bind value, without formatting: 然后,将其作为绑定值传递给Spring,而无需格式化:
return template
.queryForInt(
"SELECT COUNT(TRANSDATE) FROM stockdata
WHERE TRANSDATE= ? AND symbolname = ?",
rowMapper, date, SymbolName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.