logo

Java Calendar set() Metode

Set()-metoden for Calendar-klassen bruges til at indstille det angivne kalenderfelt med den angivne værdi.

Syntaks

Denne metode er overbelastet på følgende måder.

 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

Mark - kalenderfeltet.

Værdi ? denne beståede værdi er indstillet til specificeret kalenderfelt som parameter.

Vender tilbage

AT

Kaster

AT

Eksempel 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)); } } 
Test det nu

Produktion:

 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 

Eksempel 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()); } } 
Test det nu

Produktion:

 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