- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 9316 Articles for Object Oriented Programming
3K+ 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
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 += "'Minutes = '"; strDate += "mm"; strDate += "'Seconds = '"; strDate += "ss";Now, create an object and format the date −SimpleDateFormat dateFormat = new SimpleDateFormat(strDate); String formattedDate = dateFormat.format(new Date());The following is an example −Example Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String ... Read More
123 Views
To set a duration, let us declare two objects of Calendar classCalendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance();Set a time for one of the calendar objectsc2.add(Calendar.HOUR, 9); c2.add(Calendar.MINUTE, 15); c2.add(Calendar.SECOND, 40);Now, find the difference between both the time. One would be the current time and another we declared above −long calcSeconds = (c2.getTimeInMillis() - c1.getTimeInMillis()) / 1000;The following is an example −Example Live Demoimport java.util.Calendar; public class Demo { public static void main(String args[]) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); // set hour, minute and second c2.add(Calendar.HOUR, 9); ... Read More
2K+ Views
Firstly, import the following Java packages −import java.text.SimpleDateFormat; import java.util.Date;Now, create objects −Date dt = new Date(); SimpleDateFormat dateFormat;Displaying date in the format we wantdateFormat = new SimpleDateFormat("E MMM dd yyyy");The following is an example −Example Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String args[]) { Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("E MMM dd yyyy"); System.out.println("Date: "+dateFormat.format(dt)); } }OutputDate: Thu Nov 22 2018
2K+ Views
Firstly, import the following Java packagesimport java.text.SimpleDateFormat; import java.util.Date;Now, create objectsDate dt = new Date(); SimpleDateFormat dateFormat;Displaying date in the format we want −dateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");The following is an example −Example Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String args[]) { Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz"); System.out.println("Date: "+dateFormat.format(dt)); } }OutputDate: 22 Nov 2018 07:53:58 UTC
93 Views
For default format, use −DateFormat.getDateInstance(DateFormat.DEFAULT)To parse the string date value, use the parse() methodDate dt = DateFormat.getDateInstance(DateFormat.DEFAULT).parse("Nov 19, 2018");The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; public class Demo { public static void main(String[] argv) throws Exception { // parse date Date dt = DateFormat.getDateInstance(DateFormat.DEFAULT).parse("Nov 19, 2018"); System.out.println("Date: "+dt); } }OutputDate: Mon Nov 19 00:00:00 UTC 2018
316 Views
In Java, you can parse string date time value input usingSimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");We have used the above class since we imported the following package −import java.text.SimpleDateFormat;Now, we can display the date in the same format −Date dt = (Date) dateFormatter.parseObject("Tue, 20 Nov 2018 16:10:45 -0530");The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { Format dateFormatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); Date dt = (Date) dateFormatter.parseObject("Tue, 20 Nov 2018 16:10:45 -0530"); ... Read More
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; import java.text.SimpleDateFormat; public class Demo { public static void main(String[] argv) throws Exception { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); // parse System.out.println("Parse Date and Time..."); Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15"); System.out.println(dt); } }OutputParse ... Read More
147 Views
UseSimpleDateFormat('dd-MMM-yy') for string date.Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");For above class, do not forget to import the following package, else an error would be visible.import java.text.SimpleDateFormat;Now, parse the date.Date dt = (Date) dateFormatter.parseObject("20-Nov-18");Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { Format dateFormatter = new SimpleDateFormat("dd-MMM-yy"); // parse Date dt = (Date) dateFormatter.parseObject("20-Nov-18"); System.out.println("Date = "+dt); } }OutputDate = Tue Nov 20 00:00:00 UTC 2018
154 Views
To get the time format, use the DateFormat class and create a new object.DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a");Now, parse the time.dateFormatter.parse("12.55.20 PM");Example Live Demoimport java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a"); System.out.println("Parse Time..."); Date dt = (Date) dateFormatter.parse("12.55.20 PM"); System.out.println(dt); } }OutputParse Time... Thu Jan 01 12:55:20 UTC 1970