Java Long Number to Time getTimeOf12(long time)

Here you can find the source of getTimeOf12(long time)

Description

get Time Of

License

Apache License

Declaration

private static String getTimeOf12(long time) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    private static String getTimeOf12(long time) {
        return getAmPm(time) + " " + getTimeStr(getHourOf12(time), getMinute(time), true);
    }// w w w  .  ja v a2s .c om

    private static String getAmPm(long time) {
        SimpleDateFormat sf = new SimpleDateFormat("aa");
        return sf.format(new Date(time));
        // return getAmPmStr(time) == 0 ? mContext.getString(AM) : mContext.getString(PM);
    }

    private static String getTimeStr(int hour, int minute, boolean is12) {
        StringBuilder temp = new StringBuilder();
        if (is12) {
            temp.append(hour);
        } else {
            temp.append(toTwoDigits(hour));
        }
        temp.append(":");
        temp.append(toTwoDigits(minute));
        return temp.toString();
    }

    private static int getHourOf12(long time) {
        int hour = currentDateTime(time, Calendar.HOUR);
        if (hour == 0)
            hour = 12;
        return hour;
    }

    private static int getMinute(long time) {
        return currentDateTime(time, Calendar.MINUTE);
    }

    private static String toTwoDigits(int digit) {
        if (digit >= 10) {
            return "" + digit;
        } else {
            return "0" + digit;
        }
    }

    private static int currentDateTime(long time, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return calendar.get(type);
    }
}

Related

  1. getTime(Long time)
  2. getTime(long time, boolean getTimeOnly, boolean fileSystemSafe)
  3. getTimeFromLong(long diff)
  4. getTimeFromLong(long diff)
  5. getTimeLong()
  6. getTimeOld(long time)
  7. getTimestampDiff(Long old)
  8. getTimeStr(long t)
  9. getTimeStr(long timeLong)