转换时间值格式化“HH:MM AM / PM”采用Android [英] Convert time value to format “hh:mm Am/Pm” using Android

查看:2409
本文介绍了转换时间值格式化“HH:MM AM / PM”采用Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的日期值从数据库中,如2013年2月27日6时06分三十零秒 使用的StringTokenizer我会得到的时间分别如下图所示。

 字符串的startTime =2013年2月27日6点06分30秒;

StringTokenizer的令牌=新的StringTokenizer(startTime时);
                字符串DATE1 = token.nextToken();
                字符串时间1 = token.nextToken();
 

和在时间1我得到的结果六时06分30秒,

我可以重新储存于String类型的另一个变量如下?

 字符串displayValue =上午6点06分;
 

如果时间1变量的值

 字符串时间= 16:00:00;
 

那么就应该转换为:

 字符串displayValue =下午4点00分;
 

解决方案

我得到了答案只是在做这样的。

 的startTime =2013年2月27日二十一时06分30秒;
                    StringTokenizer的TK =新的StringTokenizer(startTime时);
                    字符串日期= tk.nextToken();
                    字符串时间= tk.nextToken();

                    SimpleDateFormat的SDF =新的SimpleDateFormat(HH:MM:SS);
                    SimpleDateFormat的SDFS =新的SimpleDateFormat(HH:MM一);
                    日期DT;
                    尝试 {
                        DT = sdf.parse(时间);
                        的System.out.println(时间显示:+ sdfs.format(DT)); //<  - 我在这里产生
                    }赶上(ParseException的E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }
 

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";

解决方案

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) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

这篇关于转换时间值格式化“HH:MM AM / PM”采用Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆