Java Articles - Page 102 of 540

How to calculate elapsed/execution time in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:13:27

9K+ Views

In general, the elapsed time or, execution is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.The now() method ... Read More

How to measure execution time for a Java method?

Maruthi Krishna
Updated on 02-Sep-2023 15:19:24

63K+ Views

In general, the elapsed time is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.The now() method of the ... Read More

How to measure elapsed time in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:09:38

5K+ Views

In general, the elapsed time is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −Using the currentTimeMillis() methodThe currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.ExampleLive Demopublic class Example {    public void test(){ int num = 0;       for(int i=0; i

How to use formatting with printf() correctly in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:07:25

375 Views

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.Following table lists the various format characters to format time by the Java printf() method along with their description −Format CharactersDescription'H'The corresponding argument is formatted as Hour of the day (00-24).'I'The corresponding argument is formatted as hour of the day (01 -12).'k'The corresponding argument is formatted ... Read More

Mobile

How to format time using printf() method in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:06:28

270 Views

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.Following table lists the various format characters to print date printf() method along with their description −Format CharactersDescription'B'The corresponding argument is formatted as full month name.'b'The corresponding argument is formatted as abbreviated month name.'h'The corresponding argument is formatted as abbreviated month name.'A'The corresponding argument is formatted as ... Read More

How to print date using GregorianCalendar class in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:00:20

1K+ Views

The GregorianCalendar class supports standard calendars it supports Julian and Gregorian calendars you can create an object of GregorianCalendar using one of its constructors. Following are various examples demonstrating how to print date using this class −ExampleFollowing example creates GregorianCalander by passing year, month and date values as parameters to its constructor and prints the date −Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Test {    public static void main(String args[]){       //Instantiating the GregorianCalendar       GregorianCalendar cal = new GregorianCalendar(2018, 6, 27);       System.out.println(cal);       System.out.println("Date: "+cal.get(Calendar.DATE));       System.out.println("Month: ... Read More

How to parse date sting to date in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:59:55

2K+ Views

You can parse a string containing a data to date value using the following ways −The constructor SimpleDateFormat class accepts a String value representing the desired date format and creates this object. You can parse the date string using the parse() method of this class.The parse() method of the LocalDate class accepts a String value representing a date and returns a LocalDate object.The DateUtils provides utility to format date you can find it in apache.commons package. The parseDate() method of the DateUtils class accepts a format string and a date string as parameters and returns a Date object.The parse() method of ... Read More

How to compare two dates in String format in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:49:19

8K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string.Parse the date string using the parse() method.The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two datesExampleOnce you create date objects from strings you can compare them using either of these methods as shown below −Live Demoimport java.text.ParseException; ... Read More

What are various ways to compare time values in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:48:47

2K+ Views

The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.ExampleLive Demoimport java.time.LocalTime; public class Test {    public static void main(String args[]) {         LocalTime Time1 = LocalTime.of(10, 15, 45);       LocalTime Time2 = LocalTime.of(07, 25, 55);             Boolean bool1 = Time1.isAfter(Time2);         Boolean bool2 = Time1.isBefore(Time2);       if(bool1){          System.out.println(Time1+" is after "+Time2);       }else if(bool2){          System.out.println(Time1+" ... Read More

How do I create a java.sql.Date object in Java?

Maruthi Krishna
Updated on 05-Feb-2021 10:41:48

6K+ Views

Using the ConstructorThe java.sql.Date represents the date value in JDBC. The constructor of this class accepts a long value representing the desired date and creates respective Date object.Date(long date)You can create this object using this constructor.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; public class Demo {    public static void main(String args[]) throws ParseException {         String str = "26-09-1989";       SimpleDateFormat obj = new SimpleDateFormat("dd-MM-yyyy");             long epoch = obj.parse(str).getTime();             System.out.println("Date value: "+epoch);       //Creating java.util.Date object       java.util.Date date = ... Read More

Advertisements