logo

Metoda Java Calendar set().

Metoda set() razreda Calendar se uporablja za nastavitev podanega polja koledarja s podano vrednostjo.

Sintaksa

Ta metoda je preobremenjena na naslednje načine.

 public void set(int field, int value) public final void set(int year, int month, int date) public final void set(int year,int month, int date, int hourOfDay, int minute) public final void set(int year, int month, int date, int hourOfDay, int minute, int second) 

Parameter

polje - koledarsko polje.

Vrednost ? ta posredovana vrednost je kot parameter nastavljena na določeno polje koledarja.

Vračila

TO

Meti

TO

Primer 1

 import java.util.Calendar; public class JavaCalendarsetExample1 { public static void main(String[] args) { // create a calendar object mycalobj Calendar mycalobj = Calendar.getInstance(); System.out.println('Today :' + mycalobj.getTime()); System.out.println('Year :' + mycalobj.get(Calendar.YEAR)); System.out.println('Month :' + mycalobj.get(Calendar.MONTH)); System.out.println('Day :' + mycalobj.get(Calendar.DAY_OF_WEEK)); // set the year,month and day mycalobj.set(1996, 5, 23, 10, 15, 20); System.out.println('Now Today :' + mycalobj.getTime()); System.out.println(' Now Year :' + mycalobj.get(Calendar.YEAR)); System.out.println('Now Month :' + mycalobj.get(Calendar.MONTH)); System.out.println('Now Day :' + mycalobj.get(Calendar.DAY_OF_WEEK)); } } 
Preizkusite zdaj

Izhod:

 Today :Tue Aug 14 17:53:46 PDT 2018 Year :2018 Month :7 Day :3 Now Today :Sun Jun 23 10:15:20 PDT 1996 Now Year :1996 Now Month :5 Now Day :1 

Primer 2

 import java.util.Calendar; public class JavaCalendarsetExample2 { public static void main(String[] args) { // create a calendar object mycalobj Calendar mycalobj = Calendar.getInstance(); System.out.println('Today :' + mycalobj.getTime()); mycalobj.set(Calendar.YEAR , 1996); System.out.println(' Altered Date(1) :' + mycalobj.getTime()); mycalobj.set (1996 , 9 , 23); System.out.println(' Altered Date (2) :' + mycalobj.getTime()); } } 
Preizkusite zdaj

Izhod:

 Today :Tue Aug 14 17:58:18 PDT 2018 Altered Date(1) :Wed Aug 14 17:58:18 PDT 1996 Altered Date (2) :Wed Oct 23 17:58:18 PDT 1996