-1

我对 TimeOfDay 对象有一个目标。我需要帮助编写 getTimeForDay 方法。稍后将提供代码。我想编写此方法,以便给定一个日历对象,它返回一个具有相同年、月和日的新日历对象,但具有与此实例关联的时间。代码是:

import java.util.Calendar;

public class TimeOfDayImpl implements TimeOfDay,Cloneable,Comparable<TimeOfDay> {

    private byte minute;
    private byte hour;
    public TimeOfDayImpl(byte min,byte hr) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int compareTo(TimeOfDay o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public byte hour() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public byte minute() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Calendar getTimeForDay(Calendar referenceDay) {
        referenceDay.getInstance();
        referenceDay.
        // TODO Auto-generated method stub
        return null;
    }

    public TimeOfDay clone() throws CloneNotSupportedException 
    {
        return (TimeOfDay) super.clone();
    }
}
4

1 回答 1

0

您应该将Java Docs作为您的第一个调用点。一点点努力就会有很长的路要走;)。如果您已经尝试过查看它们,请确保说明您在找到答案时遇到了什么问题,因为,是的,它们有时会非常令人困惑:P

public Calendar getTimeForDay(Calendar referenceDay) {
    // Create a new Calendar, which should equal "NOW"
    Calendar timeOfDay = Calendar.getInstance();
    // Transfer the year, month and date values from our reference calendar
    // to our "NOW" calendar...
    // Include our hour and minute values, not that I'm zeroing out the seconds
    timeOfDay.set(
            referenceDay.get(Calendar.YEAR), 
            referenceDay.get(Calendar.MONTH), 
            referenceDay.get(Calendar.DATE),
            (int)hour(),
            (int)minute(),
            0);
    // Return the result
    return timeOfDay;
}
于 2013-08-15T04:47:15.247 回答