Samual Sam has Published 2492 Articles

Java Program to format date with System.out.format

Samual Sam

Samual Sam

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

88 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 ... Read More

Java Program to set date formats for different countries

Samual Sam

Samual Sam

Updated on 27-Jun-2020 13:21:27

339 Views

Firstly, set the locale −Locale[] strLocales = { US, UK, GERMANY};Now, let us set the date formats i.e. different constants −int[] constants = { SHORT, MEDIUM, LONG }; String[] str = { "SHORT", "MEDIUM", "LONG" };Loop through and get the different date formats for different countries −for (Locale country : ... Read More

Java Program to format time using Custom Format

Samual Sam

Samual Sam

Updated on 27-Jun-2020 13:20:01

137 Views

Firstly, set the time with SimpleDateFormat classFormat dateFormat = new SimpleDateFormat("h:m:s");Now, for custom format, let us fetch the hour, minute and second individuallyHour// hour dateFormat = new SimpleDateFormat("h"); String strHour = dateFormat.format(new Date()); System.out.println("Hour: "+strHour);Minute// minute dateFormat = new SimpleDateFormat("m"); String strMinute = dateFormat.format(new Date()); System.out.println("Minute: "+strMinute);Second// second dateFormat = ... Read More

Java Program to list Short Month Names

Samual Sam

Samual Sam

Updated on 27-Jun-2020 13:15:02

1K+ Views

To list short months, use the getShortMonths() from the DateFormatSymbols class in Java.DateFormatSymbols is a class for encapsulating localizable date-time formatting data.Get short month names in an arrayString[] months = new DateFormatSymbols().getShortMonths();Display the monthsfor (int i = 0; i < months.length - 1; i++) { String month = months[i]; System.out.println("Month ... Read More

Java Program to display Time in 12-hour format

Samual Sam

Samual Sam

Updated on 27-Jun-2020 13:06:15

2K+ Views

Use the SimpleDateFormat class to display time in 12-hour format.Set the formatDate dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("hh:mm:ss a");Now, the following will display time in 12-hour formatdateFormat.format(dt)The following is an exampleExample Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo {    public static void main(String[] argv) throws ... Read More

Java Program to format date with SimpleDateFormat

Samual Sam

Samual Sam

Updated on 27-Jun-2020 13:02:02

177 Views

The SimpleDateFormat class is used to parse and format dates. To create an object, firstly import the following packageimport java.text.SimpleDateFormat;Set the dateString strDate = "'Year = '"; strDate += "yyyy"; strDate += "'Month = '"; strDate += "MMMMMMMMM"; strDate += "'Hours = '"; strDate += "hh"; strDate += "a"; strDate ... Read More

Java Program to format date with DateFormat.FULL

Samual Sam

Samual Sam

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

257 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; ... Read More

Java Program to format time with DateFormat.MEDIUM

Samual Sam

Samual Sam

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

151 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); ... Read More

Format time with DateFormat.FULL in Java

Samual Sam

Samual Sam

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

355 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 = ... Read More

Java Program to parse date and time

Samual Sam

Samual Sam

Updated on 27-Jun-2020 12:47:00

183 Views

Create a SimpleDateFormat object −SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");Do not forget to import the following package for SimpleDateFormat class −import java.text.SimpleDateFormat;Now, since we have set the format for date above, let us parse the date using parseObject() method −Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15");The following is an example −Example Live Demoimport java.util.Date; ... Read More

Advertisements