Found 9150 Articles for Object Oriented Programming

Convert from float to String in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:21:49

9K+ Views

To convert float to string, use the toString() method. It represents a value in a string.Let’s say the following is our float.float f = 0.9F;Converting the float value to string.String s = Float.toString(f);Let us see the complete example to convert float to String in Java with output.Example Live Demopublic class Demo {    public static void main(String args[]) {       float f = 0.9F;       String s = Float.toString(f);       System.out.println(s);    } }Output0.9

Java Program to return a Date set to the first possible millisecond of the day after midnight

Samual Sam
Updated on 26-Jun-2020 08:24:18

130 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

Java Program to return a Date set to the last possible millisecond of the day before midnight

karthikeya Boyini
Updated on 26-Jun-2020 08:23:20

152 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

Format and Parse Date in Java

Samual Sam
Updated on 26-Jun-2020 08:33:29

317 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

Display weekday names in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:34:35

481 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

Determine day of week in month from Gregorian Calendar in Java

Samual Sam
Updated on 26-Jun-2020 08:35:26

317 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

Java Program to display previous day from GregorianCalender

karthikeya Boyini
Updated on 26-Jun-2020 08:36:23

421 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

Java program to display previous month from GregorianCalendar

Samual Sam
Updated on 25-Jul-2024 19:36:40

3K+ Views

The GregorianCalendar class in Java allows us to manipulate dates and times easily. In this example, we will create a GregorianCalendar object and use it to display the current date and the date of the previous month. Problem Statement Write a Java program and create a GregorianCalendar object, display the current date, and then modify this date to show the date of the previous month. Output Current date: Mon Nov 19 18:07:03 UTC 2018 Input Modified date (Previous Month): Fri Oct 19 18:07:03 UTC 2018 Steps to display previous month from GregorianCalendar Below are the steps to display previous ... Read More

Java Program to return a Date set to noon to the closest possible millisecond of the day

Revathi Satya Kondra
Updated on 14-Jan-2025 03:35:32

334 Views

To set a Date object to noon to the closest possible millisecond of the day, we will make sure that the hour is set to noon, and both the minutes, seconds, and milliseconds are set to zero. This precise setting can be achieved using the Calendar class in Java. By setting these specific fields, we can ensure that the time is 12:00:00.000 PM, providing the closest possible millisecond representation of noon. Returning a date set to noon to the closest possible millisecond of the day in Java is quite easy. Let's learn the following ways: ... Read More

Java Program to convert Java String to Float Object

Samual Sam
Updated on 26-Jun-2020 08:11:35

298 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

Advertisements