在添加时间时需要以24小时格式获取时间

Aru*_*mar 14 android

我已经编写了一个添加时间的函数,如下所示

private void Delay15Minute() {
        String pkManifest = manifest.pkManifestNo;
        manifest_helper = new manifest_helper(this);
        cursor = manifest_helper.GetDeliveries(pkManifest);
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.getString(cursor.getColumnIndex("PKDelivery"));
            // String
            // RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime"));
            String RevisedTime = "12:55";

            // get hour and minute from time string
            StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
            int j = 0;
            int[] val = new int[st1.countTokens()];
            // iterate through tokens
            while (st1.hasMoreTokens()) {
                val[j] = Integer.parseInt(st1.nextToken());
                j++;
            }
            // call time add method with current hour, minute and minutesToAdd,
            // return added time as a string
            String date = addTime(val[0], val[1], 15);
            // Tioast the new time
            Toast.makeText(this, "date is =" + date, Toast.LENGTH_SHORT).show();

        }
            }



public String addTime(int hour, int minute, int minutesToAdd) {
        Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
        calendar.add(Calendar.MINUTE, minutesToAdd);
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        String date = sdf.format(calendar.getTime());

        return date;
    }
Run Code Online (Sandbox Code Playgroud)

我在01:10得到了这个12小时的结果...

我需要以13:10格式获得它,即24小时格式.....请帮助我

小智 50

hh在你的SimpleDateFormat模式中使用过.这是12小时的格式.kk相反,请使用24小时格式为您提供一天中的小时数.请参阅SimpleDateFormat.

  • 如果您使用**kk**,您将获得**24:30**的结果,但如果您使用**HH**,您可以得到**00:30**. (7认同)

dd6*_*619 40

只需创建实例Calendar并获得24小时的时间,

Calendar c = Calendar.getInstance();

int Hr24=c.get(Calendar.HOUR_OF_DAY);
int Min=c.get(Calendar.MINUTE);
Run Code Online (Sandbox Code Playgroud)


小智 7

使用此代码

long date = System.currentTimeMillis();

SimpleDateFormat date1 = new SimpleDateFormat("dd-MM-yyyy"); // for current date
SimpleDateFormat time1 = new SimpleDateFormat("kk:mm:ss");  // for 24 hour time
SimpleDateFormat time2 = new SimpleDateFormat("hh:mm:ss");  // for 12 hour time 

String dateString = date1.format(date);    //This will return current date in 31-12-2018 format
String timeString1 = time1.format(date);  //This will return current time in 24 Hour format
String timeString2 = time2.format(date);  //This will return current time in 12 Hour format

Log.e("TAG_1", "24 hour Time - " + timeString1);
Log.e("TAG_1", "24 hour Time - " + timeString1);
Log.e("TAG_1", "dd-MM-yyyy Date format - " + dateString);
Run Code Online (Sandbox Code Playgroud)

而不是打开您logcat的检查结果。