promotion image of download ymail app
Promoted

JAVA關於時間問題??

Time abc= new Time(23,59);

代表23點59分

可否能把abc變為只有分鐘,沒有小時??

是什麼COMMAND??

1 Answer

Rating
  • p
    Lv 6
    1 decade ago
    Favorite Answer

    The Time class has many deprecated methods, which do not include one to extract minutes.

    The Calendar abstract class is generally used for time calcualtions. Here are some examples of its use. If you want to have just the minutes and not hours, you can create your own class for that purpose. The examples below include one which extracts the current minute as an int.

    If you need more info, PM or post supplementary questions.

    import java.util.Calendar;

    import java.sql.Time;

    public class TestTime

    {

    public static void setTime(Calendar cal, int hour, int min)

    {

    cal.set(cal.HOUR_OF_DAY,hour);

    cal.set(cal.MINUTE,min);

    }

    public static int getMinute(Calendar cal)

    {

    return cal.get(Calendar.MINUTE);

    }

    public static void main(String[] args)

    {

    Calendar now=Calendar.getInstance();

    System.out.printf("Minutes=%d\n",now.get(Calendar.MINUTE));

    // Time abc=new Time(23,59,0);

    Calendar abc=Calendar.getInstance();

    setTime(abc,23,59);

    System.out.printf("Minutes=%d\n",abc.get(Calendar.MINUTE));

    System.out.printf("Minutes=%d\n",getMinute(abc));

    }

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.