Metóda set() triedy Calendar sa používa na nastavenie zadaného poľa kalendára o zadanú hodnotu.
Syntax
Táto metóda je preťažená nasledujúcimi spôsobmi.
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
lúka - pole kalendára.
Hodnota ? táto odovzdaná hodnota je nastavená na špecifikované pole kalendára ako parameter.
Návraty
TO
hádže
TO
Príklad 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)); } }Vyskúšajte to
Výkon:
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
Príklad 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()); } }Vyskúšajte to
Výkon:
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