And*_*ner 31 android stringtokenizer android-date
我使用StringTokenizer从数据库获取日期值如"2013-02-27 06:06:30"我将分别得到时间,如下所示
String startTime = "2013-02-27 06:06:30";
StringTokenizer token = new StringTokenizer(startTime);
String date1 = token.nextToken();
String time1 = token.nextToken();
Run Code Online (Sandbox Code Playgroud)
在时间1我得到的结果06:06:30,
我可以将它重新存储在String类型的另一个变量中,如下所示吗?
String displayValue = "06:06 AM";
Run Code Online (Sandbox Code Playgroud)
如果time1变量的值为
String time = 16:00:00;
Run Code Online (Sandbox Code Playgroud)
那么它应该转换为:
String displayValue = "04:00 PM";
Run Code Online (Sandbox Code Playgroud)
Mur*_*gan 52
试试这个..
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
Run Code Online (Sandbox Code Playgroud)
And*_*ner 27
我得到了这样的回答.
startTime = "2013-02-27 21:06:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();
String time = tk.nextToken();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {
dt = sdf.parse(time);
System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
mao*_*eng 16
首先,您不需要使用StringTokenizer来获取字符串时间.就这样通过你startTime:
// Get date from string
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormatter.parse(startTime);
// Get time from date
SimpleDateFormat timeFormatter = new SimpleDateFormat("h:mm a");
String displayValue = timeFormatter.format(date);
// Done!
Run Code Online (Sandbox Code Playgroud)
dha*_*ram 10
试试这个
String time = "22:35";
try {
SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
找出此链接http://developer.android.com/reference/java/text/SimpleDateFormat.html
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
Run Code Online (Sandbox Code Playgroud)
输出2.00上午
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
String time1 = sdf.format(dt);
Run Code Online (Sandbox Code Playgroud)
输出2.00 AM
我建议使用DateFormat,比如SimpleDateFormat
try {
String timeLong = "2013-02-27 06:06:30";
String timeShort = "16:06 AM";
SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
Log.v("out", formatShort.format(formatLong.parse(timeLong)));
Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我今天很累,我觉得我在这段代码中遗漏了一些东西,所以我可能会稍后修改它,但它确实有效,并且它没有(直接)调用已弃用的Date类.
1假设您需要以09:30 PM的格式显示当前时间.这将是一个相当简单的方法.即使您当前不需要它,您也应该能够根据您的要求使用下面的DateFormat.
Calendar cal = Calendar.getInstance();
DateFormat outputFormat = new SimpleDateFormat("KK:mm a");
String formattedTime = outputFormat.format(cal.getTime());
Run Code Online (Sandbox Code Playgroud)
2 注意:以下格式化程序可用于以24小时格式(21:30)显示相同的时间.
new SimpleDateFormat("HH:mm");
Run Code Online (Sandbox Code Playgroud)
3但是,如果要构造与我的第一点相同的格式,最好使用以下代码,因为Java不鼓励使用StringTokenizer.你可以在这里阅读它,http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
希望这对你有所帮助,谢谢!
String startTime = "2013-02-27 21:06:30";
String[] parts = startTime.split(" ");
DateFormat outputFormat = new SimpleDateFormat("KK:mm a");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm:ss");
try {
Date dt = parseFormat.parse(parts[1]);
System.out.println(outputFormat.format(dt));
} catch(ParseException exc) {
exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79978 次 |
| 最近记录: |