Object Oriented Programming Articles

Page 574 of 589

How to get number of quarters between two dates in Java

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 718 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

Read More

Java Program to convert java.util.Date to any local date in certain timezone

Samual Sam
Samual Sam
Updated on 30-Jul-2019 250 Views

First, set the Date and ZoneId −Date date = new Date(); ZoneId zone = ZoneId.systemDefault();Now convert the java.util.date to localdate −date.toInstant().atZone(zone).toLocalDate() date.toInstant().atZone(zone).toLocalTime() date.toInstant().atZone(zone).getHour() date.toInstant().atZone(zone).getMinute() date.toInstant().atZone(zone).getSecond()Exampleimport java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       Date date = new Date();       ZoneId zone = ZoneId.systemDefault();       System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());       System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());       System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());       System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());       System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());    } }OutputLocalDate = 2019-04-18 LocalTime= 23:25:09.708 Hour = 23 Minute = 25 Seconds = 9

Read More

Java Program to convert java.util.Date to java.time.LocalDateTime

Samual Sam
Samual Sam
Updated on 30-Jul-2019 276 Views

First, set the date −java.util.Date date = new Date();Now, convert the above Date to java.time.LocalDateTime −java.time.LocalDateTime dateTime =    java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());Exampleimport java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       java.time.LocalDateTime dateTime =          java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());       System.out.println("LocalDateTime = "+dateTime);    } }OutputDate = Thu Apr 18 23:39:34 IST 2019 LocalDateTime = 2019-04-18T23:39:34.400

Read More

Java Program to create custom DateTime formatter

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 256 Views

To create custom DateTime formatter, use DateTimeFormatter. Let us first see for Time −DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter();For Date −dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("/") .appendValue(ChronoField.MONTH_OF_YEAR) .appendLiteral("/") .appendValue(ChronoField.DAY_OF_MONTH) .toFormatter();Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; public class Demo {    public static void main(String[] args) {       DateTimeFormatter dtFormat = new DateTimeFormatterBuilder()       .appendValue(ChronoField.HOUR_OF_DAY)       .appendLiteral(":")       .appendValue(ChronoField.MINUTE_OF_HOUR)       .appendLiteral(":")       .appendValue(ChronoField.SECOND_OF_MINUTE)       .toFormatter();       System.out.println("Time = "+dtFormat.format(LocalDateTime.now()));       dtFormat = new DateTimeFormatterBuilder()     ...

Read More

How to create IntSummaryStatistics from Collectors in Java?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 327 Views

Let us first create a List:List emp = Arrays.asList(    new Employee("John", "Marketing", 5),    new Employee("David", "Operations", 10));We have class Employee with name, department and rank of employees.Now, create summary for the rank like count, average, sum, etc:IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));The following is an example to create IntSummaryStatistics from Collectors in Java:Exampleimport java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo {    public static void main(String[] args) throws Exception {       List emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));       IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → ...

Read More

How to change font size with HTML in Java Swing JEditorPane?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 1K+ Views

Use HTMLEditorKitt to change the font size with HTML. With that, use the JEditorPane setText() method to set HTML:HTMLEditorKit kit = new HTMLEditorKit(); editorPane.setEditorKit(kit); editorPane.setSize(size); editorPane.setOpaque(true); editorPane.setText(" This is a demo text with a different font!");The following is an example to change font size with HTML in Java Swing JEditorPane:Exampleimport java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.text.html.HTMLEditorKit; public class SwingDemo extends JFrame {    public static void main(String[] args) {       SwingDemo s = new SwingDemo();       s.setSize(600, 300);       Container container = s.getContentPane();       s.demo(container, container.getSize());   ...

Read More

Java Program to check if any String in the list starts with a letter

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 6K+ Views

First, create a List with String elements:List myList = new ArrayList(); myList.add("pqr"); myList.add("stu"); myList.add("vwx"); myList.add("yza"); myList.add("bcd"); myList.add("efg"); myList.add("vwxy");Use the startsWith() method to check if any of the above string in the myList begins with a specific letter:myList.stream().anyMatch((a) -> a.startsWith("v"));TRUE is returned if any of the string begins with the specific letter, else FALSE.The following is an example to check if any String in the list starts with a letter:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List myList = new ArrayList();       myList.add("pqr");       myList.add("stu"); ...

Read More

How to add items in a JComboBox on runtime in Java

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 2K+ Views

The following is an example to add items on runtime on a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {         ...

Read More

How to handle action event for JComboBox in Java?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 928 Views

The following is an example to handle action event for JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             ...

Read More

Can we disable JComboBox arrow button in Java?

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 714 Views

Yes, we can do that using removeArrow() method.The following is an example to disable JComboBox arrow button:Exampleimport java.awt.Component; import java.awt.Container; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       String[] strValues = {"One", "Two"};       JComboBox comboBox = new JComboBox(strValues);       removeArrow(comboBox);       JOptionPane.showMessageDialog(null, comboBox);    }    private static void removeArrow(Container container) {       Component[] c = container.getComponents();       for (Component res : c) {          if (res instanceof AbstractButton) {             container.remove(res);          }       }    } }Output

Read More
Showing 5731–5740 of 5,881 articles
« Prev 1 572 573 574 575 576 589 Next »
Advertisements