JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

How to convert string to Date Java 8?


This page shows examples on how to convert string to date object using Java8 new date and time APIs.

StringToDateEx
package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class StringToDateEx {

	public static void main(String[] args) {

		// if date is ISO_LOCAL_DATE format it is very straight conversion
		// means string date is in "yyyy-mm-dd" format
		String dateStr = "2015-11-18";
		LocalDate dtObj = LocalDate.parse(dateStr);
		System.out.println("Date object: "+dtObj);

		// convert it using formatter
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
		LocalDate fdtObj = LocalDate.parse("18/11/2015", dateFormatter);
		System.out.println("Date object: "+fdtObj);

		// convert 18 Nov 2015
		DateTimeFormatter dateFormatter1 = DateTimeFormatter.ofPattern("dd MMM yyyy");
		LocalDate fdtObj1 = LocalDate.parse("18 Nov 2015", dateFormatter1);
		System.out.println("Date object: "+fdtObj1);
	}
}

Output:
Date object: 2015-11-18
Date object: 2015-11-18
Date object: 2015-11-18
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.