Found 272 Articles for Java8

CountDownLatch in Java

Janani Jaganathan
Updated on 13-Oct-2022 11:25:00

3K+ Views

For a concurrent execution, CountDownLatch in Java is an important class that ensures one or more threads are in the queue for other threads to complete their set of operations. To better understand CountDownLatch in Java, in this article, you will learn the working of CountDownLatch with an example and methods of CountDownLatch. CountDownLatch in Java and its Working Process Based on the count value, the CountDownLatch is used for several purposes, which are as follows − When we begin the CountDownlatch with count value 1, it will simply work as an on/off latch or gate. On the other ... Read More

Coupling in Java

karthikeya Boyini
Updated on 19-Jun-2020 13:22:22

4K+ Views

Coupling refers to the usage of an object by another object. It can also be termed as collaboration. This dependency of one object on another object to get some task done can be classified into the following two types −Tight coupling - When an object creates the object to be used, then it is a tight coupling situation. As the main object creates the object itself, this object can not be changed from outside world easily marked it as tightly coupled objects.Loose coupling - When an object gets the object to be used from the outside, then it is a loose coupling ... Read More

Database operations in Java

karthikeya Boyini
Updated on 19-Jun-2020 13:27:08

1K+ Views

This article provides an example of how to create a simple JDBC application. This will show you how to open a database connection, execute a SQL query, and display the results.Creating JDBC ApplicationThere are following six steps involved in building a JDBC application −Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.Register the JDBC driver: Requires that you initialize a driver so you can open a communication channel with the database.Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents ... Read More

Date Formatting Using SimpleDateFormat

karthikeya Boyini
Updated on 19-Jun-2020 12:43:03

681 Views

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.ExampleLive Demoimport java.util.*; import java.text.*; public class DateDemo {    public static void main(String args[]) {       Date dNow = new Date( );       SimpleDateFormat ft =               new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");       System.out.println("Current Date: " + ft.format(dNow));    } }This will produce the following result −OutputCurrent Date: Sun 2004.07.18 at 04:14:09 PM PDTSimple DateFormat ... Read More

Date format validation using Java Regex

Samual Sam
Updated on 19-Jun-2020 12:45:34

520 Views

Following example demonstrates how to check whether the date is in a proper format or not using matches method of String class.ExampleLive Demopublic class Main {    public static void main(String[] argv) {       boolean isDate = false;       String date1 = "8-05-1988";       String date2 = "08/04/1987";       String datePattern = "\d{1, 2}-\d{1, 2}-\d{4}";       isDate = date1.matches(datePattern);       System.out.println("Date :"+ date1+": matches with the this date Pattern:"+datePattern+"Ans:"+isDate);       isDate = date2.matches(datePattern);       System.out.println("Date :"+ date2+": matches with the this date ... Read More

Date Formatting Using printf

Samual Sam
Updated on 19-Jun-2020 12:26:09

4K+ Views

Date and time formatting can be done very easily using the printf method. You use a two-letter format, starting with t and ending in one of the letters of the table as shown in the following code.ExampleLive Demoimport java.util.Date; public class DateDemo {    public static void main(String args[]) {       // Instantiate a Date object       Date date = new Date();       // display time and date       String str = String.format("Current Date/Time : %tc", date );       System.out.printf(str);    } }This will produce the following ... Read More

Date Parsing using SimpleDateFormat

karthikeya Boyini
Updated on 19-Jun-2020 12:27:22

219 Views

The SimpleDateFormat class has parse() method, which tries to parse a string according to the format stored in the given SimpleDateFormat object.ExampleLive Demoimport java.util.*; import java.text.*;   public class DateDemo {    public static void main(String args[]) {       SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");               String input = args.length == 0 ? "1818-11-11" : args[0];         System.out.print(input + " Parses as ");               Date t;       try {          t = ft.parse(input);                    System.out.println(t);               } catch (ParseException e) {                    System.out.println("Unparseable using " + ft);               }    } }A sample run of the above program would produce the following result −Output1818-11-11 Parses as Wed Nov 11 00:00:00 EST 1818

Deadlock in Java Multithreading

Samual Sam
Updated on 19-Jun-2020 12:30:28

2K+ Views

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in a different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Here is an example.ExampleLive Demopublic class TestThread {    public static Object Lock1 = new Object();    public static Object Lock2 = new Object();    public static void main(String args[]) {       ThreadDemo1 T1 = ... Read More

Create gradient translucent windows in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:36:39

2K+ Views

With JDK 7, we can create a gradient based translucent window using swing very easily. Following are the steps needed to make a gradient-based translucent window. Make the background of JFrame transparent first.frame.setBackground(new Color(0, 0, 0, 0)); Create a gradient paint, and fill the panel.JPanel panel = new javax.swing.JPanel() {    protected void paintComponent(Graphics g) {       Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),        getWidth(), getHeight(), new Color(R, G, B, 255), true);       Graphics2D g2d = (Graphics2D)g;       g2d.setPaint(p);       g2d.fillRect(0, 0, getWidth(), getHeight());    } } Assign ... Read More

GregorianCalendar Class in Java

Samual Sam
Updated on 19-Jun-2020 12:40:18

127 Views

GregorianCalendar is a concrete implementation of a Calendar class that implements the normal Gregorian calendar with which you are familiar. We did not discuss Calendar class in this tutorial, you can look up standard Java documentation for this.The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC. These represent the two eras defined by the Gregorian calendar.There are also several constructors for GregorianCalendar objects −Sr.No.Constructor & Description1GregorianCalendar()Constructs a default GregorianCalendar using the current time in the default time zone with the default ... Read More

Previous 1 ... 3 4 5 6 7 ... 28 Next
Advertisements