如何在java中操作日期对象

如何在java中操作日期对象,java,android,datetime,Java,Android,Datetime,我想使用一个日期对象,如“Sat Feb 17 20:49:54+0000 2007”,并将年份变量动态更改为当前年份,如“Sat Feb 17 20:49:54+0000 2012”,在java中最好的方法是什么?从日期构造日历,使用日历设置年份,然后从日历中获取日期对象 Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.YEAR, 2012); date = c.getTime(); 从日期构造日历,使

我想使用一个日期对象,如“Sat Feb 17 20:49:54+0000 2007”,并将年份变量动态更改为当前年份,如“Sat Feb 17 20:49:54+0000 2012”,在java中最好的方法是什么?

从日期构造日历,使用日历设置年份,然后从日历中获取日期对象

Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.YEAR, 2012);
date = c.getTime();

从日期构造日历,使用日历设置年份,然后从日历中获取日期对象

Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.YEAR, 2012);
date = c.getTime();

如果该对象已经是日期对象,则可以执行以下操作:

Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
cal.setTime(dateObj);
//set the year to current year
cal.set(Calendar.YEAR, currentYear);
//new date object with current year
dateObj = cal.getTime();
如果是字符串,可以首先使用SimpleDataFormat将字符串解析为Java日期对象:
并使用上述日历对象。

如果该对象已经是日期对象,则可以执行以下操作:

Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
cal.setTime(dateObj);
//set the year to current year
cal.set(Calendar.YEAR, currentYear);
//new date object with current year
dateObj = cal.getTime();
如果是字符串,可以首先使用SimpleDataFormat将字符串解析为Java日期对象:
并使用上述日历对象。

根据您的要求,您可以这样做:

try {
    DateFormat dateFormat = new SimpleDateFormat("E, dd MMM HH:mm:ss Z yyyy");
    date = (Date) dateFormat.parse("Sat, Feb 17 20:49:54 +0000 2007");
    Calendar cal = dateFormat.getCalendar();
    cal.set(Calendar.YEAR, 2012);
} catch (ParseException pe) {
    //ParseException Handling
} catch(Exception e) {
    //Exception Handling
}

根据您的要求,您可以这样做:

try {
    DateFormat dateFormat = new SimpleDateFormat("E, dd MMM HH:mm:ss Z yyyy");
    date = (Date) dateFormat.parse("Sat, Feb 17 20:49:54 +0000 2007");
    Calendar cal = dateFormat.getCalendar();
    cal.set(Calendar.YEAR, 2012);
} catch (ParseException pe) {
    //ParseException Handling
} catch(Exception e) {
    //Exception Handling
}

另一种选择是利用JodaTimeAPI

import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class App
{
    public static void main( String[] args )
    {
        //Sat Feb 17 20:49:54 +0000 2007
        DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd H:m:s Z yyyy");
        DateTime dt = fmt.parseDateTime("Sat Feb 17 20:49:54 +0000 2007");
        MutableDateTime mdt = dt.toMutableDateTime();
        mdt.setYear(new DateTime().getYear());

        System.out.println(fmt.print(mdt));
    }
}

另一种选择是利用JodaTimeAPI

import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class App
{
    public static void main( String[] args )
    {
        //Sat Feb 17 20:49:54 +0000 2007
        DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd H:m:s Z yyyy");
        DateTime dt = fmt.parseDateTime("Sat Feb 17 20:49:54 +0000 2007");
        MutableDateTime mdt = dt.toMutableDateTime();
        mdt.setYear(new DateTime().getYear());

        System.out.println(fmt.print(mdt));
    }
}