Found 9150 Articles for Object Oriented Programming

How to get number of quarters between two dates in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

620 Views

Let’s say we have the following two dates −LocalDate.of(2019, 3, 20); LocalDate.of(2019, 10, 25);To get the number of quarters between the above two dates, use the QUARTER_YEARS −IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20),LocalDate.of(2019, 10, 25));Exampleimport java.time.LocalDate; import java.time.temporal.IsoFields; public class Demo {    public static void main(String[] args) {       long quarters =          IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20), LocalDate.of(2019, 10, 25));       System.out.println("Quarters between the two dates = " + quarters);    } }OutputQuarters between the two dates = 2

How do I discover the Quarter of a given Date in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

Let us first get the current date −LocalDate currentDate = LocalDate.now();Now, use the Calendar class and set the locale −Calendar cal = Calendar.getInstance(Locale.US);Now, get the month −int month = cal.get(Calendar.MONTH);Find the Quarter −int quarter = (month / 3) + 1;Exampleimport java.time.LocalDate; import java.util.Calendar; import java.util.Locale; public class Demo {    public static void main(String[] args) {       LocalDate currentDate = LocalDate.now();       System.out.println("Current Date = "+currentDate);       Calendar cal = Calendar.getInstance(Locale.US);       int month = cal.get(Calendar.MONTH);       int quarter = (month / 3) + 1;       System.out.println("Quarter = "+quarter);    } }OutputCurrent Date = 2019-04-12 Quarter = 2

Java Program to get Temporal Queries precision

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

146 Views

To get Temporal Queries precision, use the TemporalQuery interface with the precision() method of the TemporalQueries −TemporalQueryprecision = TemporalQueries.precision(); Get the precision for LocalDate: LocalDate.now().query(precision) Get the precision for LocalTime: LocalTime.now().query(precision) Get the precision for YearMonth: YearMonth.now().query(precision) Get the precision for Year: Year.now().query(precision)Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Year; import java.time.YearMonth; import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; public class Demo {    public static void main(String[] args) {       TemporalQueryprecision = TemporalQueries.precision();       System.out.println("TemporalQueries precision...");       System.out.println(LocalDate.now().query(precision));       System.out.println(LocalTime.now().query(precision));       System.out.println(LocalDateTime.now().query(precision));       System.out.println(YearMonth.now().query(precision));       System.out.println(Year.now().query(precision)); ... Read More

Java program to get the seconds since the beginning of the Java epoch

Samual Sam
Updated on 21-Oct-2024 17:41:50

843 Views

In this article, we will learn to get the seconds since the beginning of the Java epoch. To get the seconds since the beginning of epochs, you need to use Instant. The method here used is ofEpochSecond() method. The epoch is the number of seconds that have elapsed since 00::00:00 Thursday, 1 January 1970. Get the seconds with ChronoUnit.SECONDS − long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS); Steps to retrieve seconds since Epoch Following are the steps to retrieve seconds since Epoch − First, we will import the Instant and ChronoUnit classes from the java.time and java.time.temporal packages. ... Read More

Java Program to get day of week as string

Shriansh Kumar
Updated on 16-Aug-2024 07:28:17

3K+ Views

Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat. In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date. Using LocalDate Class In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day ... Read More

Java Program to get timezone id strings

Samual Sam
Updated on 30-Jul-2019 22:30:25

223 Views

Let us get the timezone id strings for timezone America. For this, use the get.AvailableZoneIds() method −ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America")) .forEach(System.out::println);With that, we have used the forEach to display all the timezones in America −America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon . . .Exampleimport java.time.ZoneId; public class Demo {    public static void main(String[] args) {       System.out.println("TimeZones in the US");       ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America"))       .forEach(System.out::println);    } }OutputTimeZones in the US America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon America/Argentina/Catamarca America/Grand_Turk America/Argentina/Cordoba America/Araguaina America/Argentina/Salta America/Montevideo America/Manaus ... Read More

Java program to get milliseconds between dates

karthikeya Boyini
Updated on 05-Sep-2024 11:22:45

2K+ Views

In this article, we will learn to get milliseconds between dates in Java. We will be using the LocalDateTime class from java.time package and ChronoUnit.MILLIS of java.time.temporal package. ChronoUnit is an enum that is part of Java date and time API which represents a unit of time that is days, hours, minutes etc. Here, the MILLIS is a unit that represents the concept of a millisecond. Problem Statement Write a program in Jave to get milliseconds between dates. Below is the representation − Input Date One = 2024-09-04T05:40:19.817038951Date Two = 2019-04-10T11:20 Output Milliseconds between two dates = -170533219817 Steps to get milliseconds between dates ... Read More

Java Program to get the value stored in a byte as an unsigned integer

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

278 Views

Let us first create signed byte −byte signedVal = -100;Now convert a byte to an unsigned integer −int unsignedVal = Byte.toUnsignedInt(signedVal);Examplepublic class Demo {    public static void main(String[] args) {       byte signedVal = -100;       int unsignedVal = Byte.toUnsignedInt(signedVal);       System.out.println("Signed value (byte) = " + signedVal);       System.out.println("Unsigned value (byte) = " + unsignedVal);    } }OutputSigned value (byte) = -100 Unsigned value (byte) = 156

Java program to generate n distinct random numbers

Alshifa Hasnain
Updated on 22-Jan-2025 15:48:12

978 Views

In this article, we will learn to generate n distinct random numbers within a specified range, ensuring that no number is repeated in Java. Generating random numbers is a common problem in various applications such as simulations, games, and testing. Problem Statement In this problem, we are given a number n, and we need to generate n unique random numbers within a defined range, ensuring that no number is repeated. The range can be specified by the user, or it can be a default range based on the problem's requirements. Input int n = 10; Output [4, 6, 9, 1, ... Read More

Java Program to create Stream from a String/Byte Array

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

592 Views

Create an input stream and set the string:DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));The getBytes() method is used to convert a string into sequence of bytes and returns an array of bytes.Now return one single input byte:(char) inputStream.readByte()Exampleimport java.io.ByteArrayInputStream; import java.io.DataInputStream; public class Demo { public static void main(String[] args) throws Exception {    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));       System.out.print((char) inputStream.readByte());       System.out.print((char) inputStream.readByte());       inputStream.close();    } }OutputPq

Advertisements