简体   繁体   English

使用Android将时间值转换为格式“hh:mm Am / Pm”

[英]Convert time value to format “hh:mm Am/Pm” using Android

I am getting date value from database like "2013-02-27 06:06:30" using StringTokenizer I will get time separately like below 我使用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(); 

and in time1 I am getting the result 06:06:30, 在时间1我得到的结果06:06:30,

Can I re-store it in another variable of type String as follows? 我可以将它重新存储在String类型的另一个变量中,如下所示吗?

String displayValue = "06:06 AM";

And if time1 variable has the value of 如果time1变量的值为

String time = 16:00:00;

then it should be converted to: 那么它应该转换为:

String displayValue = "04:00 PM";

Try this.. 试试这个..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);

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();
}

First you don't need to use StringTokenizer to get the string time. 首先,您不需要使用StringTokenizer来获取字符串时间。 Just pass your startTime like this: 像这样传递你的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!

Try this 试试这个

 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();
}

Trace out this link http://developer.android.com/reference/java/text/SimpleDateFormat.html 找出此链接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);

output 2.00 am 输出2.00上午

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

output 2.00 AM 输出2.00 AM

I recommend using a DateFormat, like SimpleDateFormat 我建议使用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();
}

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. 我今天很累,我觉得我在这段代码中遗漏了一些东西,所以我可能会稍后修改它,但它确实有效,并且它没有(直接)调用已弃用的Date类。

1 Assuming you need to show the current time in the format 09:30 PM . 1假设您需要以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. 即使您当前不需要它,您也应该能够根据您的要求使用下面的DateFormat。

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 ). 2 注意:以下格式化程序可用于以24小时格式( 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 . 3但是,如果要构造与我的第一点相同的格式,最好使用以下代码,因为Java不鼓励使用StringTokenizer You can read about it here, http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html 你可以在这里阅读它, 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();
    }

Just use the following pattern you will get your expected result: 只需使用以下模式即可获得预期结果:

    try {
        String time = "06:06:30";
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Date dt = sdf.parse(time);

        SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
        String formatedTime = sdfs.format(dt);

        Log.v("parseTime", formatedTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

Happy coding :) 快乐编码:)

在Android上你也有DateFormat

Selected answer had an issue of showing wrong time. 选定的答案有一个显示错误时间的问题。 If your time is 12:30:00 it shows 12:30 AM instead 12:30 PM . 如果你的时间是12:30:00,那就是12:30 AM而不是12:30 PM The below code will help to overcome the issue. 以下代码将有助于克服此问题。

SimpleDateFormat sdf = new SimpleDateFormat("KK:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");

    Date dt1 = null;

    try {
        dt1 = sdf.parse("12:00:00");
        Log.i("Time in Am Pm  ", sdfs.format(dt1));

    } catch (ParseException e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM