Java Articles - Page 630 of 745

Date Formatting Using SimpleDateFormat

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

1K+ 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 Formatting Using SimpleDateFormat

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

1K+ 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

852 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 format validation using Java Regex

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

852 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

7K+ 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 Formatting Using printf

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

7K+ 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

398 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

Date Parsing using SimpleDateFormat

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

398 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

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

Advertisements