Found 7442 Articles for Java

Set Date patterns with SimpleDateFormat in Java

karthikeya Boyini
Updated on 27-Jun-2020 13:22:33

576 Views

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved) for Date and Time in JavaReference − Oracle JavaLetterDate or Time ComponentPresentationExamplesGEra designatorTextADYYearYear1996; 96YWeek yearYear2009; 09MMonth in yearMonthJuly; Jul; 07WWeek in yearNumber27WWeek in monthNumber2DDay in yearNumber189DDay in monthNumber10FDay of week in monthNumber2EDay name in weekTextTuesday; TueUDay number of week (1 = Monday, ..., 7 = Sunday)Number1AAm/pm markerTextPMHHour in day (0-23)Number0KHour in day (1-24)Number24KHour in am/pm (0-11)Number0hHour in am/pm (1-12)Number12mMinute in hourNumber30sSecond in minuteNumber55SMillisecondNumber978zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00ZTime zoneRFC 822 time zone-800XTime zoneThe above pattern letters are combined ... Read More

Java Program to format date with System.out.format

Samual Sam
Updated on 27-Jun-2020 13:23:12

159 Views

System.out.format is used in Java to format output.Firstly, create a Calendar object −Calendar calendar = Calendar.getInstance();Now, use theDate-Time conversion characters to get the date, month and year −System.out.format("%te %tB, %tY%n", calendar, calendar, calendar);The following is the complete example −Example Live Demoimport java.util.Locale; import java.util.Calendar; public class TestFormat {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.format("%te %tB, %tY%n", calendar, calendar, calendar);    } }Output22 November, 2018

Format Date with getDateTimeInstance() in Java

karthikeya Boyini
Updated on 27-Jun-2020 12:54:00

552 Views

To format date, let us see the DateFormat.SHORT as well as DateFormat.LONG constants. Both of them displays different patterns.Here, we are formatting dates with getDateTimeInstance() method. Use the method to get a date and time format.For DateFormat.SHORT constant −DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); System.out.println(dateFormat.format(calendar.getTime()));For DateFormat.LONG constant −dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); System.out.println(dateFormat.format(calendar.getTime()));The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Calendar; public class Demo {    public static void main(String s[]) {       Calendar calendar = Calendar.getInstance();       // for SHORT date-time       DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);       System.out.println(dateFormat.format(calendar.getTime()));       ... Read More

Format time with DateFormat.FULL in Java

Samual Sam
Updated on 27-Jun-2020 12:54:58

501 Views

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.FULL is a constant for full style pattern.Firstly, we will create Date objectDate dt = new Date(); DateFormat dateFormat;Let us format time for different locale with DateFormat.FULLdateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt));The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {    public static void main(String args[]) {       Date dt ... Read More

Java Program to format time with DateFormat.LONG

karthikeya Boyini
Updated on 27-Jun-2020 12:55:38

240 Views

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.LONG is a constant for long style pattern.Firstly, we will create Date objectDate dt = new Date(); DateFormat dateFormat;Let us format time for different locale with DateFormat.LONGdateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.FRENCH); System.out.println("Locale FRENCH = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.GERMANY); System.out.println("Locale GERMANY = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt));The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {    public static void main(String args[]) {       Date dt ... Read More

Java Float isInfinite() Method

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

128 Views

The Float.isInfinite() method in Java returns true if this Float value is infinitely large in magnitude, else false is returned.We have the following Float valuesFloat f1 = new Float(5.0/0.0); Float f2 = new Float(10.2/0.0); Float f3 = new Float(0.0/0.0);Check with isInfinite() methodf1.isInfinite(); f2.isInfinite(); f3.isInfinite();The following is the complete example with output −Example Live Demoimport java.lang.*; public class Demo {    public static void main(String args[]) {       Float f1 = new Float(5.0/0.0);       Float f2 = new Float(10.2/0.0);       Float f3 = new Float(0.0/0.0);       System.out.println(f1.isInfinite());       System.out.println(f2.isInfinite());       System.out.println(f3.isInfinite());    } }Outputtrue true false

Java Program to format time with DateFormat.MEDIUM

Samual Sam
Updated on 27-Jun-2020 12:58:00

267 Views

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.MEDIUM is a constant for medium style pattern.Firstly, we will create a Date object −Date dt = new Date(); DateFormat dateFormat;Let us format time for a different locale with DateFormat.MEDIUMdateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt));The following is an exampleExample Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {    public static void main(String args[]) {       Date dt = new Date();       DateFormat dateFormat; ... Read More

Format time with DateFormat.SHORT in Java

karthikeya Boyini
Updated on 27-Jun-2020 12:58:35

562 Views

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.SHORT is a constant for short style pattern.Firstly, we will create Date objectDate dt = new Date(); DateFormat dateFormat;Let us format time for different locale with DateFormat.SHORTdateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt));The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {    public static void main(String args[]) {       Date dt = new Date();       DateFormat dateFormat;   ... Read More

Java Program to format date with DateFormat.FULL

Samual Sam
Updated on 27-Jun-2020 12:59:07

370 Views

DateFormat.FULL is a constant for full style pattern.Firstly, we will create Date object −Date dt = new Date(); DateFormat dateFormat;Let us format date for different locale with DateFormat.FULL −// FRENCH dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRENCH); // CANADA dateFormat = DateFormat.getDateInstance(DateFormat. FULL, Locale.CANADA);The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {       public static void main(String args[]) {       Date dt = new Date();       DateFormat dateFormat;       // Date Format FULL constant       dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRENCH);       System.out.println("Locale FRENCH = " ... Read More

Format date with DateFormat.SHORT in Java

karthikeya Boyini
Updated on 27-Jun-2020 13:00:29

4K+ Views

DateFormat.SHORT is a constant for short style pattern.Firstly, we will create date objectDate dt = new Date(); DateFormat dateFormat;Let us format date for different locale with DateFormat.SHORT// FRENCH dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH); // GERMANY dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo {    public static void main(String args[]) {       Date dt = new Date();       DateFormat dateFormat; // Date Format SHORT constant       dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH);       System.out.println("Locale FRENCH = " + dateFormat.format(dt));       dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, ... Read More

Advertisements