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

前端 未结 10 1947
粉色の甜心
粉色の甜心 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: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.

提交回复
热议问题