- 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 34484 Articles for Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
251 Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Here is the object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, let us get the past date, using the add() method with a negative value.// past date cal.add((GregorianCalendar.DATE), -1);Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past date cal.add((GregorianCalendar.DATE), -1); System.out.println("Modified date (Previous Date): " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 18:01:37 UTC 2018 Modified date (Previous Month): Fri ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
114 Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous year.cal.add((GregorianCalendar.YEAR), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // previous year cal.add((GregorianCalendar.YEAR), -1); System.out.println("Modified date: " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 18:05:49 UTC 2018 Modified date: Sun Nov 19 18:05:49 UTC 2017
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
690 Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Firstly, let us display the current date and time.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime());Now, modify the date. Here we are adding two days to the month using the add() method.cal.add((GregorianCalendar.MONTH), 2);Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); cal.add((GregorianCalendar.MONTH), 2); System.out.println("Modified date: " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 17:52:55 UTC 2018 Modified date: Sat Jan ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
290 Views
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar calendar = new GregorianCalendar();To get the day of the week, use the following field.GregorianCalendar.DAY_OF_WEEKThe following is an example.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar calendar = new GregorianCalendar(); System.out.println("Day of Week = " + calendar.get(GregorianCalendar.DAY_OF_WEEK)); System.out.println("Date = " + calendar.get(GregorianCalendar.DATE)); System.out.println("Month = " + calendar.get(GregorianCalendar.MONTH)); System.out.println("Year = " + calendar.get(GregorianCalendar.YEAR)); } }OutputDay of Week = 2 Date = 19 Month = 10 Year = ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
302 Views
To work with the GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Firstly, create a GregorianCalendar object.GregorianCalendar calendar = new GregorianCalendar();Now, set the above-created object to a date. Here 0 is for 1st month.calendar.set(2018, 0, 25);The following is an example.Example Live Demoimport java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; public class Demo { public static void main(String[] args) { GregorianCalendar calendar = new GregorianCalendar(); // 0 is for 1st month calendar.set(2018, 0, 25); System.out.println("" + calendar.getTime()); } }OutputThu Jan 25 16:51:24 UTC 2018
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
To work with the GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create a Date object.Date d = new Date();Now, create an object and set the time using the setTime() method.GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d);The following is an example.Example Live Demoimport java.util.Date; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { Date d = new Date(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d); System.out.println(d); } }OutputMon Nov 19 16:11:31 UTC 2018