Metoda set() klase Calendar koristi se za postavljanje navedenog polja kalendara prema navedenoj vrijednosti.
Sintaksa
Ova metoda je preopterećena na sljedeće 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)
Parametar
polje - polje kalendara.
Vrijednost ? ova proslijeđena vrijednost postavljena je na navedeno polje kalendara kao parametar.
Povratak
DA
Bacanja
DA
Primjer 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)); } }Testirajte sada
Izlaz:
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
Primjer 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()); } }Testirajte sada
Izlaz:
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