
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

2K+ Views
In this article, we will learn to list up the short month names in Java. To do so we will be using DateFormatSymbols class from java.text package. java.text: This package offers classes and interfaces for managing text, dates, numbers, and messages in a way that is not dependent on any specific language. DateFormatSymbols is a class that helps you work with date and time information that can be adapted to different languages and regions. It includes things like the names of the months, the days of the week, and details about time zones. Problem Statement Write a Java program that lists ... Read More

161 Views
For date formatting symbols, use the DateFormatSymbols class.Firstly, set weekdays using a string arrayString[] weekDays2 = { "", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };Now, set short week days withDateFormatSymbols mySymbols = new DateFormatSymbols(); mySymbols.setShortWeekdays(weekDays2);Get the date −dateFormat = new SimpleDateFormat("EEEE, dd MMM yyyy", mySymbols); System.out.println(dateFormat.format(new Date()));The following is an example −Example Live Demoimport java.text.SimpleDateFormat; import java.text.DateFormat; import java.text.DateFormatSymbols; import java.util.Date; public class Demo { public static void main(String[] args) { String[] weekDays1 = { "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; String[] weekDays2 = { "", "monday", "tuesday", "wednesday", "thursday", ... Read More

221 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 = new SimpleDateFormat("s"); String strSecond = dateFormat.format(new Date()); System.out.println("Second: "+strSecond);The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] argv) throws Exception { Format dateFormat = new SimpleDateFormat("h:m:s"); String str = dateFormat.format(new Date()); ... Read More

3K+ Views
Firstly, set the format with SimpleDateFormat classFormat dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy");Above, the “EEE” is set to display the name of the day i.e. Monday, Tuesday, Wednesday, etc.Now, to display the date −String res = dateFormat.format(new Date());The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] argv) throws Exception { Format dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy"); String res = dateFormat.format(new Date()); System.out.println("Date = " + res); } }OutputDate = Thu, 22/11/2018

449 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 : strLocales) { System.out.println(""+ country.getDisplayCountry() + "....."); for (int i = 0; i < constants.length; i++) { dateFormat = DateFormat.getDateInstance(constants[i], country); System.out.println(str[i] + " constant = " + dateFormat.format(dt)); } }The following is an exampleExample Live Demoimport static java.text.DateFormat.*; import static java.util.Locale.*; import ... Read More

575 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

157 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

550 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

500 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

238 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