Found 7442 Articles for Java

Get the last day of month in Java

Samual Sam
Updated on 27-Jun-2020 09:05:14

14K+ Views

The getActualMaximum() method returns the maximum value that the specified calendar field could have, based on the time value of this Calendar.Import the following package to work with Calendar class in Java, import java.util.Calendar;Create a calendar object.Calendar cal = Calendar.getInstance();Use the getActualMaximum() method to get the last day of the month.int res = cal.getActualMaximum(Calendar.DATE);The following is an example.Example Live Demoimport java.util.Calendar; public class Main {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       int res = cal.getActualMaximum(Calendar.DATE);       System.out.println("Today's Date = " + cal.getTime());       System.out.println("Last Date of ... Read More

Get current time information in Java

karthikeya Boyini
Updated on 27-Jun-2020 08:50:59

267 Views

Import the following package for to work with Calendar class in Java, import java.util.Calendar;Create a calendar class now.Calendar cal = Calendar.getInstance();To display entire time information, use the following fields.cal.get(Calendar.HOUR_OF_DAY) cal.get(Calendar.HOUR) cal.get(Calendar.MINUTE) cal.get(Calendar.SECOND) cal.get(Calendar.MILLISECOND)The following is the final example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // time information       System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR));     ... Read More

Display Month of Year using Java Calendar

karthikeya Boyini
Updated on 27-Jun-2020 08:53:25

4K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Using the Calendar class, create an object.Calendar calendar = Calendar.getInstance();Now, create a string array of the month names.String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };Display the month name.month[calendar.get(Calendar.MONTH)]The following is an example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };       System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]); ... Read More

Display Day Name of Week using Java Calendar

Samual Sam
Updated on 27-Jun-2020 08:55:52

4K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Using the Calendar class, create an object.Calendar calendar = Calendar.getInstance();Now, create a string array of the day names.String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };Display the day name.days[calendar.get(Calendar.DAY_OF_WEEK) - 1]The following is the final example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Day: " + (calendar.get(Calendar.DATE)));       System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));       System.out.println("Year: " + (calendar.get(Calendar.YEAR)));       String[] days = new String[] ... Read More

Get Day Number of Week in Java

karthikeya Boyini
Updated on 27-Jun-2020 08:57:07

11K+ Views

To get the day of the week, use Calendar.DAY_OF_WEEK.Firstly, declare a calendar object and get the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toString());Now, fetch the day of the week in an integer variable.int day = calendar.get(Calendar.DAY_OF_WEEK);The following is the final example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println(calendar.getTime().toString());       int day = calendar.get(Calendar.DAY_OF_WEEK);       System.out.println("Day: " + day);       int hour = calendar.get(Calendar.HOUR_OF_DAY);       System.out.println("Hour: " + hour);       int minute = ... Read More

Java Program to display Current Time in another Time Zone

Shriansh Kumar
Updated on 30-Sep-2024 15:54:09

2K+ Views

A time zone is a region of the globe that observes a uniform standard time for legal, commercial, and social purposes. Suppose you have an application that is running in India as well as Japan. Here, you can't use the same time zone for both regions. Therefore, it is necessary to display time in different time zones. Java provides various built-in classes like TimeZone and ZoneId and methods like getTimeZone() that can help in displaying the current time in another time zone. However, before using them, you must import them into your Java program. Using getTimeZone() and setTimeZone() Methods ... Read More

Create a Date object using the Calendar class in Java

Aishwarya Naglot
Updated on 11-Nov-2024 19:35:20

2K+ Views

In Java, we can create a date object using the calendar class, which gives us more control on date and time. We will discuss the process in this article. What is the Calendar class? In order to understand the usage of we must know what is the calendar class. The Calendar class allows us to work with date and time more easily than Date class. like, we can set specific parts of the date, like month, year, and day. For using Calendar class, import the following package. import java.util.Calendar; Now, let us create an object of Calendar class. Calendar ... Read More

Get the Day of the Week from Today's Date in Java

Samual Sam
Updated on 27-Jun-2020 08:47:20

481 Views

To get the day of the week, use the Calendar.DAY_OF_WEEK.Firstly, let us get the current date.java.util.Date utilDate = new java.util.Date(); java.sql.Date dt = new java.sql.Date(utilDate.getTime());Now, using GregorianCalendar, set the time.java.util.GregorianCalendar cal = new java.util.GregorianCalendar(); cal.setTime(dt);The last step would display the day of the week as shown in the following example.Example Live Demoimport java.text.ParseException; public class Demo {    public static void main(String[] args) throws ParseException {       java.util.Date utilDate = new java.util.Date();       java.sql.Date dt = new java.sql.Date(utilDate.getTime());       System.out.println("Today's date: "+dt);       java.util.GregorianCalendar cal = new java.util.GregorianCalendar();       cal.setTime(dt);   ... Read More

Java Program to convert Date into milliseconds

karthikeya Boyini
Updated on 27-Jun-2020 08:48:17

4K+ Views

Import the following package to work with Date class.import java.util.Date;No create a Date object.Date d = new Date();Let us convert the current date to milliseconds.d.getTime()The following is an example.Example Live Demoimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.println("Date = " + d);       System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT = " + d.getTime());    } }OutputDate = Mon Nov 19 06:30:11 UTC 2018 Milliseconds since January 1, 1970, 00:00:00 GMT = 1542609011369

Convert the Current Time to a java.sql.Date Object

Samual Sam
Updated on 27-Jun-2020 08:49:02

648 Views

Firstly, create a Calendar class object.Calendar calendar = Calendar.getInstance();Now, import the following package.import java.sql.Date;Using a Date class now and creating an object would belong to the above package. Convert the current time to the java.sql.Date Object.Date sqlDate = new Date((calendar.getTime()).getTime());The following is an example.Example Live Demoimport java.util.Calendar; import java.sql.Date; import java.text.ParseException; public class Demo {    public static void main(String[] args) throws ParseException {       Calendar calendar = Calendar.getInstance();       // object       Date sqlDate = new Date((calendar.getTime()).getTime());       System.out.println(sqlDate);    } }Output2018-11-19Read More

Advertisements