- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
76 Views
Let us first set the calendar object.Calendar calendar = Calendar.getInstance();Use the getMinimum() method in Java to return the minimum value for the given calendar field. We will use it to set the minute, hours second and milliseconds.For hour and minute.calendar.set(Calendar.HOUR_OF_DAY, calendar.getMinimum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getMinimum(Calendar.MINUTE));For second and milliseconds.// second calendar.set(Calendar.SECOND, calendar.getMinimum(Calendar.SECOND)); // millisecond calendar.set(Calendar.MILLISECOND, calendar.getMinimum(Calendar.MILLISECOND));The following is an example that returns a Date set to the first possible millisecond of the day after midnight.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar calendar = Calendar.getInstance(); // ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
76 Views
Let us first set the calendar object.Calendar calendar = Calendar.getInstance();Use the getMaximum() method in Java to returns the maximum value for the given calendar field. We will use it to set the minute, hours second and milliseconds.For hour and minute.calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));For second and milliseconds.// second calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND)); // millisecond calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));The following is an example that returns a Date set to the last possible millisecond of the day before midnight.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar calendar = Calendar.getInstance(); // ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
261 Views
To format a date in Java, firstly import the following package.import java.text.DateFormat;Now, create DateFormat object.DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);Use the format() method to format the above dates.System.out.println(shortFormat.format(new Date())); System.out.println(longFormat.format(new Date()));To parse the dates, use the parse() method.Example Live Demoimport java.text.DateFormat; import java.text.ParseException; import java.util.Date; public class Demo { public static void main(String[] args) throws Exception { // format date DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG); System.out.println("Format Date..."); System.out.println(shortFormat.format(new Date())); System.out.println(longFormat.format(new Date())); // parse date ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
384 Views
To display weekday names in Java, use the getWeekdays() method.For that, firstly import the following package.import java.text.DateFormatSymbols;Now, create a string array and get all the weekday names using the getWeekdays() method.String[] weekDays = new DateFormatSymbols().getWeekdays();Example Live Demoimport java.text.DateFormatSymbols; public class Demo { public static void main(String[] args) { String[] weekDays = new DateFormatSymbols().getWeekdays(); System.out.println("Weekday names..."); for(String days: weekDays){ System.out.println(days); } } }OutputWeekday names... Sunday Monday Tuesday Wednesday Thursday Friday Saturday
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
222 Views
To work with the GregorianCalendar class, import the following package −import java.util.GregorianCalendar;To get the day of week in month, use the following field −cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)Above, cal is the GregorianCalendar object we created before −GregorianCalendar cal = new GregorianCalendar();The following is an example −Example Live Demoimport java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; public class Demo { public static void main(String[] args) { GregorianCalendar cal = new GregorianCalendar(); // date information System.out.println("Date Information.........."); System.out.println("Year = " + cal.get(Calendar.YEAR)); System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1)); System.out.println("Date ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
304 Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous day.cal.add((GregorianCalendar.DATE), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past date cal.add((GregorianCalendar.DATE), -1); System.out.println("Modified date (Previous Day): " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 18:12:53 UTC 2018 Modified date (Previous Day): Sun Nov ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous month.cal.add((GregorianCalendar.MONTH), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past month cal.add((GregorianCalendar.MONTH), -1); System.out.println("Modified date (Previous Month): " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 18:07:03 UTC 2018 Modified date (Previous Month): Fri Oct ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
219 Views
Use the getMinimum() method in Java to returns the minimum value for the given calendar field. We will use it to set the minute, second and millisecond.Let us first declare a calendar object.Calendar dateNoon = Calendar.getInstance();Now, we will set the hour, minute, second and millisecond to get the closest possible millisecond of the day.dateNoon.set(Calendar.HOUR_OF_DAY, 12); dateNoon.set(Calendar.MINUTE, dateNoon.getMinimum(Calendar.MINUTE)); dateNoon.set(Calendar.SECOND, dateNoon.getMinimum(Calendar.SECOND)); // millisecond dateNoon.set(Calendar.MILLISECOND, dateNoon.getMinimum(Calendar.MILLISECOND));Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar dateNoon = Calendar.getInstance(); // hour, minute and second dateNoon.set(Calendar.HOUR_OF_DAY, 12); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
178 Views
To convert String to Float Object, you can use a method Float.valueOf() method or you can even achieve this without using the in-built method.Let us see both the examples.The following is an example that converts String to Float Object using Float.valueOf() method.Example Live Demopublic class Demo { public static void main(String args[]) { Float f = Float.valueOf("30.67"); System.out.println(f); } }Output30.67Let us see another example.Example Live Demopublic class Demo { public static void main(String args[]) { Float f = new Float("45.88"); System.out.println(f); } }Output45.88
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
342 Views
Let us first declare a Float object.Float ob = new Float("29.35");Now, let us convert it to numeric primitive data type short using shortValue()short val1 = ob.shortValue();In the same way, we can convert it to int using intValue() method.short val1 = ob.intValue();The following is an example that converts Float to other numeric primitive data types as well.Example Live Demopublic class Demo { public static void main(String args[]) { Float ob = new Float("29.35"); // numeric primitive data types short val1 = ob.shortValue(); System.out.println(val1); int val2 = ... Read More