Convert time value to format “hh:mm Am/Pm” using A

2019-01-06 21:44发布

I am getting date value from database like "2013-02-27 06:06:30" using StringTokenizer I will get time separately like below

String startTime = "2013-02-27 06:06:30";

StringTokenizer token = new StringTokenizer(startTime);
String date1 = token.nextToken();  
String time1 = token.nextToken(); 

and in time1 I am getting the result 06:06:30,

Can I re-store it in another variable of type String as follows?

String displayValue = "06:06 AM";

And if time1 variable has the value of

String time = 16:00:00;

then it should be converted to:

String displayValue = "04:00 PM";

9条回答
戒情不戒烟
2楼-- · 2019-01-06 22:21

Try this..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
查看更多
Animai°情兽
3楼-- · 2019-01-06 22:21

1 Assuming you need to show the current time in the format 09:30 PM. This would be a fairly easy approach. Even if you don't require it for the current time, you should be able to use the below DateFormat for your requirement.

Calendar cal = Calendar.getInstance();
DateFormat outputFormat = new SimpleDateFormat("KK:mm a");
String formattedTime = outputFormat.format(cal.getTime());

2 Note: The following formatter can be used to display the same time in 24-hour format (21:30).

new SimpleDateFormat("HH:mm");

3 However, if you want to construct the same format as in my first point, it is best to use the following code as Java discourages the use of StringTokenizer. You can read about it here, http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html

Hope this would help you, thanks!

    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();
    }
查看更多
\"骚年 ilove
4楼-- · 2019-01-06 22:26

On Android you also have DateFormat

查看更多
我命由我不由天
5楼-- · 2019-01-06 22:31

I got answer just doing like this.

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();
}
查看更多
走好不送
6楼-- · 2019-01-06 22:32

I recommend using a DateFormat, like 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();
}

I am tired today and I feel like I am missing something in this code so I might amend it later, but it does work and it doesn't (directly) call the deprecated Date class.

查看更多
Root(大扎)
7楼-- · 2019-01-06 22:32
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);

output 2.00 a.m.

 Date dt = new Date(date1);
 SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
 String time1 = sdf.format(dt);

output 2.00 AM

查看更多
登录 后发表回答