Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较小时数(字符串与小时数和实际小时数)_Java_Android_Date - Fatal编程技术网

Java 比较小时数(字符串与小时数和实际小时数)

Java 比较小时数(字符串与小时数和实际小时数),java,android,date,Java,Android,Date,我试图将实际的小时和分钟与这样的字符串“18:00”进行比较。我得到的实际小时和分钟如下: Calendar now = Calendar.getInstance(); int hour = now.get(Calendar.HOUR); int minute = now.get(Calendar.MINUTE); date = parseDate(hour + ":" + minute); dateCompareOne = parseDate(c.getString(TAG_HORA))

我试图将实际的小时和分钟与这样的字符串“18:00”进行比较。我得到的实际小时和分钟如下:

Calendar now = Calendar.getInstance();

int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);

date = parseDate(hour + ":" + minute);
dateCompareOne = parseDate(c.getString(TAG_HORA));
if ((dateCompareOne.after(date))){//...
我有一个类似“18:00”(TAG_HORA)的字符串,我试图比较两个类似这样的日期:

Calendar now = Calendar.getInstance();

int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);

date = parseDate(hour + ":" + minute);
dateCompareOne = parseDate(c.getString(TAG_HORA));
if ((dateCompareOne.after(date))){//...

我没有结果,有人能帮我吗?谢谢

您可以使用
String.split()
Integer.valueOf()从
TAG\u HORA
中提取小时和分钟。那么,比较时间就很容易了

Calendar now = new GregorianCalendar();
int nowHour = now.get(Calendar.HOUR);
int nowMin = now.get(Calendar.MINUTE);


String hora = "18:00";
String[] parts = hora.split(":");

int horaHour = Integer.valueOf(parts[0]);
int horaMinute = Integer.valueOf(parts[1]);

if(60 * nowHour + nowMin > 60 * horaHour + horaMinute) {

}

您可以使用
String.split()
Integer.valueOf()从
TAG\u HORA
中提取小时和分钟。那么,比较时间就很容易了

Calendar now = new GregorianCalendar();
int nowHour = now.get(Calendar.HOUR);
int nowMin = now.get(Calendar.MINUTE);


String hora = "18:00";
String[] parts = hora.split(":");

int horaHour = Integer.valueOf(parts[0]);
int horaMinute = Integer.valueOf(parts[1]);

if(60 * nowHour + nowMin > 60 * horaHour + horaMinute) {

}

如果您不介意秒数和毫秒数,您可以按照流程执行:

Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
now.getTimeInMillis();
使用键标记ֹ(u HORA)解析值传递。
如果您不介意秒数和毫秒数,请使用=sings进行比较:

Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
now.getTimeInMillis();
使用键标记ֹ(u HORA)解析值传递。
与使用=sings进行比较相比,我希望这能有所帮助:

import java.util.Calendar;
import java.lang.Math; // headers MUST be above the first class
import javax.xml.bind.DatatypeConverter;
import static javax.xml.bind.DatatypeConverter.parseDate;

// one class needs to have a main() method
public class test
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {

    Calendar now = Calendar.getInstance();
    int hour = now.get(Calendar.HOUR);
    int minute = now.get(Calendar.MINUTE);

    Calendar date1 = Calendar.getInstance();
    date1.set(Calendar.HOUR_OF_DAY, date1.getTime().getHours() );
    date1.set(Calendar.MINUTE, date1.getTime().getMinutes() );
    date1.set(Calendar.SECOND, 0);

    String reference = "18:00";
    String[] parts = reference.split(":");
    Calendar date2 = Calendar.getInstance();
    date2.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
    date2.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
    date2.set(Calendar.SECOND, 0);

    if (date1.before(date2)) {
        System.out.println(date1.getTime()+" vs "+date2.getTime());
        System.out.println("Not yet at the limit");
    }

  }
}
编辑:输出为:

Sat Jul 01 16:05:00 GMT+01:00 2017 vs Sat Jul 01 18:00:00 GMT+01:00 2017
Not yet at the limit

我希望这有助于:

import java.util.Calendar;
import java.lang.Math; // headers MUST be above the first class
import javax.xml.bind.DatatypeConverter;
import static javax.xml.bind.DatatypeConverter.parseDate;

// one class needs to have a main() method
public class test
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {

    Calendar now = Calendar.getInstance();
    int hour = now.get(Calendar.HOUR);
    int minute = now.get(Calendar.MINUTE);

    Calendar date1 = Calendar.getInstance();
    date1.set(Calendar.HOUR_OF_DAY, date1.getTime().getHours() );
    date1.set(Calendar.MINUTE, date1.getTime().getMinutes() );
    date1.set(Calendar.SECOND, 0);

    String reference = "18:00";
    String[] parts = reference.split(":");
    Calendar date2 = Calendar.getInstance();
    date2.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
    date2.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
    date2.set(Calendar.SECOND, 0);

    if (date1.before(date2)) {
        System.out.println(date1.getTime()+" vs "+date2.getTime());
        System.out.println("Not yet at the limit");
    }

  }
}
编辑:输出为:

Sat Jul 01 16:05:00 GMT+01:00 2017 vs Sat Jul 01 18:00:00 GMT+01:00 2017
Not yet at the limit

您最好从日期中提取小时和分钟,然后将其与从TAG_HORA中提取的小时和分钟进行比较。这比伪造日期更干净。你能给我举个例子吗?thanksafter(..)返回一个布尔值。调用“After”后会发生什么?如果你没有得到一个结果,那么你得到的是一个例外。你没有做好比较,因为它返回的时间比实际时间早几个小时,我只想得到实际时间后的小时。你最好从日期中提取小时和分钟,然后将其与从TAG_HORA中提取的小时和分钟进行比较。这比伪造日期更干净。你能给我举个例子吗?thanksafter(..)返回一个布尔值。调用“After”后会发生什么?如果你没有得到一个结果,那么你就得到了一个例外,因为它返回的时间比实际时间早几个小时,我只想得到实际时间后的几个小时