0

我正在使用 Java 的 Joda DateTime 函数将 30 分钟添加到设定时间。例如 10:00:00 到 10:30:00。但是,下面的代码不起作用。主要的两个问题是
1. 当年份设置为 2012 年时,myFormat.parse 函数自动将日期设置为 2014 年(见下面的 logcat)。2. plusMinutes(30) 不是在时间上增加 30 分钟。你能看看我的代码,让我知道如何修复它吗?

Java代码:

package com.google.android.gcm.demo.app.Alerts;

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

import org.joda.time.DateTime;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.android.gcm.demo.app.R;
import com.google.android.gcm.demo.app.EventList.DataView;
import com.google.android.gcm.demo.app.EventList.EventDetails;
import com.google.android.gcm.demo.app.sqllite.DatabaseSqlite;

public class AlertDetails extends Activity {

    Integer id;
    String name;
    String date;
    String startTime;
    String endTime;
    String location;
    int alertState;
    Bundle bundle;
    String alertTime;
    String s;

    private String update_alarmTime;
    String eventYear;
    String eventDay;
    String eventMonth;

    DatabaseSqlite entry = new DatabaseSqlite(AlertDetails.this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alertview);

        bundle = getIntent().getExtras();

        alertTime = bundle.getString("alertTime");
        alertTime = "10:00:00";// for testing


        // set Date
        eventYear = date.substring(0, 4);
        eventDay = date.substring(5, 7);
        eventMonth = date.substring(8, 10);
        mDateDisplay.setText(eventDay + "-" + eventMonth + "-" + eventYear);

        String eventdate = eventDay + "/" + eventMonth + "/" + eventYear;
System.out.println("event date "+eventdate);


        String currentDate;
        SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date1 = null;
        DateTime dt;

        currentDate = eventdate+ " "+alertTime;
        System.out.println("current date "+currentDate);

        try {
            date1 = myFormat.parse(currentDate);
            System.out.println("date formated: "+date1);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        dt = new DateTime(date1);
        dt.plusMinutes(30);
        System.out.println(" origional time is " +alertTime+" new time is "+dt);

日志猫:

 02:43:16.844: D/dalvikvm(1027): GC_FOR_MALLOC freed 2421 objects / 165200 bytes in 137ms
08-09 02:43:18.355: I/ActivityManager(58): Starting activity: Intent { cmp=com.google.android.gcm.demo.app/.Alerts.AlertDetails (has extras) }
08-09 02:43:18.405: I/System.out(1027): event date 05/30/2012
08-09 02:43:18.415: I/System.out(1027): current date 05/30/2012 10:00:00
08-09 02:43:18.415: I/System.out(1027): date formated: Thu Jun 05 10:00:00 GMT+00:00 2014
08-09 02:43:18.605: D/dalvikvm(1027): GC_FOR_MALLOC freed 2743 objects / 261528 bytes in 43ms
08-09 02:43:18.665: D/dalvikvm(1027): GC_FOR_MALLOC freed 216 objects / 242000 bytes in 43ms
08-09 02:43:18.905: D/dalvikvm(1027): GC_FOR_MALLOC freed 1461 objects / 314440 bytes in 45ms
08-09 02:43:19.305: D/dalvikvm(1027): GC_FOR_MALLOC freed 3043 objects / 276440 bytes in 55ms
08-09 02:43:19.945: D/dalvikvm(1027): GC_FOR_MALLOC freed 5365 objects / 354056 bytes in 54ms
08-09 02:43:19.955: I/System.out(1027):  origional time is 10:00:00 new time is 2014-06-05T10:00:00.000Z
4

1 回答 1

3

1) 为什么没有添加 30 分钟?
方法dt.plusMinutes(30);不向dt实例添加 30 分钟。实例dt不可变的。方法dt.plusMinutes(30);创建的新实例DateTime大于dt30 分钟。
所以:

dt = new DateTime(date1);
dt.plusMinutes(30);   

是错的!
应该

dt = new DateTime(date1).plusMinutes(30);   

或者

dt = new DateTime(date1);
DateTime greaterDate = dt.plusMinutes(30);  

2) 为什么年份 = 2012?

你的模式是"dd/MM/yyyy HH:mm:ss"
你的日期是"05/30/2012 10:00:00"
30个月?:-)
改变模式"MM/dd/yyyy HH:mm:ss"

于 2012-08-09T07:51:01.140 回答