Found 346 Articles for Java Programming

Date format validation using Java Regex

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

542 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

224 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

134 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

Create shaped windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 11:58:25

655 Views

With JDK 7, we can create a shaped window using swing very easily. Following are the steps needed to make a shaped window. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } });ExampleSee the example below of a shaped window.import java.awt.Color; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.RoundRectangle2D; import ... Read More

Create a simple calculator using Java Swing

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

18K+ Views

Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. It is built on top of AWT API and acts as a replacement of AWT API since it has almost every control corresponding to AWT controls.Following example showcases a simple calculator application.import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator implements ActionListener {    private static JTextField inputBox;    Calculator(){}    public static void main(String[] args) {       createWindow();    } ... Read More

Sleeping for a while in Java

Samual Sam
Updated on 19-Jun-2020 12:04:25

634 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Create Toast Message in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:08:08

883 Views

A toast message is an alert which disappears with time automatically. With JDK 7, we can create a toast message similar to an alert on android very easily. Following are the steps needed to make a toast message.Make a rounded rectangle shaped frame. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } ... Read More

Advertisements