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

前端 未结 10 1912
粉色の甜心
粉色の甜心 2020-11-28 13:10

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         


        
相关标签:
10条回答
  • 2020-11-28 13:41

    On Android you also have DateFormat

    0 讨论(0)
  • 2020-11-28 13:42

    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!
    
    0 讨论(0)
  • 2020-11-28 13:43

    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();
        }
    
    0 讨论(0)
  • 2020-11-28 13:43

    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.

    0 讨论(0)
  • 2020-11-28 13:45

    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();
    }
    
    0 讨论(0)
  • 2020-11-28 13:45
    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

    0 讨论(0)
提交回复
热议问题