可能重复:
在java中将时间戳转换为日期
这是我的数据库:
-
日期状态
-
1343999311 Q
- 1343892452 C
- 1343892477便士
在这里,我必须检查查询当前日期+状态=Q信息,并获得计数值。
这是我的代码:
public class TodayQ {
public int data(){
int count=0;
//count++;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","");
PreparedStatement statement = con.prepareStatement("select * from orders where status='Q' AND date=CURDATE()");
ResultSet result = statement.executeQuery();
while(result.next()) {
// Do something with the row returned.
count++; //if the first col is a count.
}
}
catch(Exception exc){
System.out.println(exc.getMessage());
}
return count;
}
}
dis是我的另一类:
public class Demo {
public static void main(String[] args) throws ParseException{
TodayQ obj = new TodayQ();
System.out.println(obj.data());
}
}
在这里,我必须编辑日期是(yyyy-mm-dd)格式。现在我得到了输出。但我希望在数据库中使用时间戳。所以如何在java中将时间戳转换为日期。如何在我的代码中使用那个代码。请帮帮我。怎么办????
您应该在代码中尝试以下操作
DateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date date = format.parse("1343993311");
这里date
对象将具有java.util.Date
对象引用。表示时间戳被转换为Date
对象。