Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet78/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   74   75   76   77   78   79   80   81   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
If you only need to obtain the current date without going into calendar details, use the 
java.util.Date class to generate a new Date object. Doing so generates a new Date 
object that is equal to the current system date. In the following code, you can see how 
easy it is to create a new Date object and obtain the current date.
Date date = new Date();
System.out.println("Using java.util.Date(): " + date);
System.out.println("Getting time from java.util.Date():
" + date.getTime());
The result is a Date object that contains the current date and time taken from the system 
that the code is run on, including the time zone information, as shown in the following 
example. The time is the number of milliseconds since January 1, 1970, 00:00:00 GMT.
Using java.util.Date(): Mon Dec 06 19:37:53 CET 2021
Getting time from java.util.Date(): 1638815873430
Chapter 4 Numbers aNd dates


129
 Solution 3
If you need to be more precise regarding the calendar, use the java.util.Calendar 
class. Although working with the Calendar class makes your code longer, the results are 
more granular than using java.util.Date. The following code demonstrates just a handful 
of the capabilities of using this class to obtain the current date.
Calendar gCal = Calendar.getInstance();
// Month is based upon a zero index, January is equal to 0,
// so we need to add one to the month for it to be in
// a standard format
int month = gCal.get(Calendar.MONTH) + 1;int day = gCal.get(Calendar.DATE);
int yr = gCal.get(Calendar.YEAR);
String dateStr = month + "/" + day + "/" + yr;
System.out.println(dateStr);
int dayOfWeek = gCal.get(Calendar.DAY_OF_WEEK);
// Print out the integer value for the day of the week
System.out.println(dayOfWeek);
int hour = gCal.get(Calendar.HOUR);
int min = gCal.get(Calendar.MINUTE);
int sec = gCal.get(Calendar.SECOND);
// Print out the time
System.out.println(hour + ":" + min + ":" + sec);
// Create new DateFormatSymbols instance to obtain the String
// value for dates
DateFormatSymbols symbols = new DateFormatSymbols();
String[] days = symbols.getWeekdays();
System.out.println(days[dayOfWeek]);
// Get crazy with the date!
int dayOfYear = gCal.get(Calendar.DAY_OF_YEAR);
System.out.println(dayOfYear);
Chapter 4 Numbers aNd dates


130
// Print the number of days left in the year
System.out.println("Days left in " + yr + ": " + (365-dayOfYear));
int week = gCal.get(Calendar.WEEK_OF_YEAR);
// Print the week of the year
System.out.println(week);
This code demonstrates that it is possible to obtain more detailed information 
regarding the current date when using the Calendar class. The results of running the 
code look like the following.
12/6/2021
2
7:37:53
lunedì
340
Days left in 2021: 25
49

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   74   75   76   77   78   79   80   81   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling