营业时间减少4小时

时间:2015-04-26 22:04:28

标签: java android simpledateformat

我试图显示两个日期之间的差异,按月,日,小时,分钟和秒进行细分。下面的代码似乎有效,但小时总是在4小时后关闭。我尝试了HOUR和HOUR_OF_DAY,但得到了相同的结果。

<servlet-class>website.HomeServlet</servlet-class>

有人认为这是因为TimeZone。我是GMT +8,DST = true所以

    Calendar cal = Calendar.getInstance();

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR);
    int minutes = cal.get(Calendar.MINUTE);
    int seconds = cal.get(Calendar.SECOND);


    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    try {
        //get the current date/time
        Date beginDate = cal.getTime();

        //fabricate a future date/time
        Date endDate = dateFormat.parse(String.valueOf(year) + "-" + String.valueOf(month)
                + "-" + String.valueOf(day+95) + " " + String.valueOf(hour+2)
                + ":" + String.valueOf(minutes +16) + ":" + String.valueOf(seconds +34));

        long millis = endDate.getTime() - beginDate.getTime();

        TextView txtMonths = (TextView) findViewById(R.id.months);
        txtMonths.setText("Months: " + (new SimpleDateFormat("M")).format(new Date(millis)));

        TextView txtDays = (TextView) findViewById(R.id.days);
        txtDays.setText("Days: " + (new SimpleDateFormat("d")).format(new Date(millis)));

        TextView txtHours = (TextView) findViewById(R.id.hours);
        txtHours.setText("Hours: " + (new SimpleDateFormat("h")).format(new Date(millis)));

        TextView txtMinutes = (TextView) findViewById(R.id.minutes);
        txtMinutes.setText("Minutes: " + (new SimpleDateFormat("m")).format(new Date(millis)));

        TextView txtSeconds = (TextView) findViewById(R.id.seconds);
        txtSeconds.setText("Seconds: " + (new SimpleDateFormat("s")).format(new Date(millis)));

    } catch (ParseException ex) {
        Log.d("ParseException", ex.toString());

    }

返回7小时而不是4.我错过了TimeZone信息吗?

1 个答案:

答案 0 :(得分:0)

这是错的 我以为我拥有它,但我没有。

我认为这是时区,但我还不确定当我是格林尼治标准时间+8和夏令时时它是如何产生4的。所以,虽然下面的代码似乎有用,但如果它已经被7或8关闭了,我会觉得更舒服。如果是这样的话,时区就会立刻突然出现。

Java可能很残酷。

    Calendar cal = Calendar.getInstance();
    TimeZone UTC = TimeZone.getTimeZone("UTC");
    cal.setTimeZone(UTC);

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR);
    int minutes = cal.get(Calendar.MINUTE);
    int seconds = cal.get(Calendar.SECOND);


    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    try {
        //get the current date/time
        Date beginDate = cal.getTime();

        //fabricate a future date/time

        endDate = dateFormat.parse(String.valueOf(year) + "-" + String.valueOf(month)
                + "-" + String.valueOf(day+95) + " " + String.valueOf(hour+2)
                + ":" + String.valueOf(minutes +1) + ":" + String.valueOf(seconds +34));
        dateFormat.setTimeZone(UTC);

        long millis = endDate.getTime() - beginDate.getTime();

        if (TimeZone.getDefault().inDaylightTime( new Date())) {
            millis += 3600000;
        }

        TextView txtMonths = (TextView) findViewById(R.id.months);
        txtMonths.setText("Months: " + (new SimpleDateFormat("M")).format(new Date(millis)));

        TextView txtDays = (TextView) findViewById(R.id.days);
        txtDays.setText("Days: " + (new SimpleDateFormat("d")).format(new Date(millis)));

        TextView txtHours = (TextView) findViewById(R.id.hours);
        txtHours.setText("Hours: " + (new SimpleDateFormat("h")).format(new Date(millis)));

        TextView txtMinutes = (TextView) findViewById(R.id.minutes);
        txtMinutes.setText("Minutes: " + (new SimpleDateFormat("m")).format(new Date(millis)));

        TextView txtSeconds = (TextView) findViewById(R.id.seconds);
        txtSeconds.setText("Seconds: " + (new SimpleDateFormat("s")).format(new Date(millis)));
    } catch (ParseException ex) {
        Log.d("ParseException", ex.toString());

    }