logo

Java Calendar set() Metode

Set()-metoden for Calendar-klassen brukes til å angi det angitte kalenderfeltet med den angitte verdien.

Syntaks

Denne metoden er overbelastet på følgende måter.

 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

felt - kalenderfeltet.

Verdi ? denne beståtte verdien er satt til spesifisert kalenderfelt som parameter.

Returnerer

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 nå

Produksjon:

 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 nå

Produksjon:

 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