Found 34494 Articles for 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

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

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

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

Format and Parse Date in Java

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

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

Display weekday names in Java

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

383 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

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

Java Program to display previous day from GregorianCalender

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

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

Java Program to display previous month from GregorianCalendar

Samual Sam
Updated on 26-Jun-2020 08:09:23

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

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

karthikeya Boyini
Updated on 26-Jun-2020 08:10:26

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

Java Program to convert Java String to Float Object

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

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

Advertisements