Joda DateTime,无法更改年份

时间:2012-08-09 03:07:42

标签: android datetime jodatime

我正在使用Joda DateTime函数为Java添加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);

logcat的:

 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

1 个答案:

答案 0 :(得分:3)

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

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"

上的模式