简体   繁体   中英

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

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

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. Just pass your startTime like this:

// 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

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

output 2.00 am

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

output 2.00 AM

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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