如何在java中仅显示日期时间组合中的时间部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26884557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display only time part from date time combination in java
提问by SRY_JAVA
I am new to Java. I am retrieving my first column from database in a String Array which represents data as:
我是 Java 新手。我正在从字符串数组中的数据库中检索我的第一列,它表示数据为:
2014-09-01 10:00:00.000
Now I want to display only time as:
现在我只想将时间显示为:
10:00:00
How to do it? My code to retrieve my Column is:
怎么做?我检索列的代码是:
public String[] getChartTime() throws SQLException {
List < String > timeStr = new ArrayList < String > ();
String atime[] = null;
getConnection();
try {
con = getConnection();
String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("date is " + df.format(currentDate));
clstmt = con.prepareCall(sql);
clstmt.setString(1, "vs3_bag");
clstmt.setString(2, "2014-09-01 10:00:00");
clstmt.setString(3, "2014-09-01 11:00:00");
clstmt.execute();
rs = clstmt.getResultSet();
while (rs.next()) {
// Just get the value of the column, and add it to the list
timeStr.add(rs.getString(1));
}
} catch (Exception e) {
System.out.println("\nException in Bean in getDbTable(String code):" + e);
} finally {
closeConnection();
}
// I would return the list here, but let's convert it to an array
atime = timeStr.toArray(new String[timeStr.size()]);
for (String s: atime) {
System.out.println(s);
}
return atime;
}
回答by Ishkafel
Use SimpleDateFormat
:
使用SimpleDateFormat
:
java.util.Date date = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(date));
If you have the date as a String, you can parse it to a java.util.Date in a step before:
如果您将日期作为字符串,则可以在之前的步骤中将其解析为 java.util.Date:
SimpleDateFormat sdf = new SimpleDateFormat("YOUR_DATE_PATTERN");
Date date = sdf.parse(string);
Use patterns according to https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
根据https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html使用模式
回答by Sagar Pudi
Refer official Java docs here
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDate {
public static void main(String[] args){
DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
Date date = new Date();
String time=dateFormat.format(date);
System.out.println(time);
}
}
回答by Johny
If I have understood the question correctly 2014-09-01 10:00:00.000
is in a string and 10:00:00
has to be extracted from that. Given below is my solution.
如果我理解正确,问题2014-09-01 10:00:00.000
是在一个字符串10:00:00
中,必须从中提取。下面给出的是我的解决方案。
- First split the string with space as the delimiter.
- Then from that take the second part of the string.
- After that split the string again using
.
as the delimiter and take the first sting from that.
- 首先用空格作为分隔符分割字符串。
- 然后从中取出字符串的第二部分。
- 之后使用
.
作为分隔符再次拆分字符串并从中取出第一个字符串。
The above things are done using the line.
上面的事情都是使用线完成的。
str.split("\s")[1].split("\.")[0];
COMPLETE CODE
完整代码
String str = new String("2014-09-01 10:00:00.000");
String time = str.split("\s")[1].split("\.")[0];
System.out.print(time);
OUTPUT
输出
10:00:00
For more details check the links given below:
有关更多详细信息,请查看下面给出的链接:
回答by SRY_JAVA
I got it,just use substring() as
我明白了,只需使用 substring() 作为
while(rs.next()) {
// Just get the value of the column, and add it to the list
timeStr.add(rs.getString(1).substring(11,16));
}