Few Java examples show you how to convert a String to the new Java 8 Date API – java.time.LocalDate

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

String date = "16/08/2016";

//convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, formatter);

Note
Refer to this official DateTimeFormatter JavaDoc for more date time formatter examples.
Note
You may interest at this classic java.util.Date example – How to convert String to Date in Java
1. String = 2016-08-16
If the String is formatted like ISO_LOCAL_DATE, you can parse the String directly, no need conversion.

TestNewDate1.java
package com.mkyong.java8.date;

import java.time.LocalDate;

public class TestNewDate1 {

public static void main(String[] argv) {

String date = "2016-08-16";

//default, ISO_LOCAL_DATE
LocalDate localDate = LocalDate.parse(date);

System.out.println(localDate);

}

}

Output

2016-08-16

2. String = 16-Aug-2016
TestNewDate2.java
package com.mkyong.java8.date;

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

public class TestNewDate2 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");

String date = "16-Aug-2016";

LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); //default, print ISO_LOCAL_DATE

System.out.println(formatter.format(localDate));

}

}

Output

2016-08-16
16-Aug-2016

3. String = 16/08/2016
TestNewDate3.java
package com.mkyong.java8.date;

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

public class TestNewDate3 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

String date = "16/08/2016";

LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate);

System.out.println(formatter.format(localDate));

}

}

Output

2016-08-16
16/08/2016

4. String = Tue, Aug 16 2016
TestNewDate4.java
package com.mkyong.java8.date;

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

public class TestNewDate4 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy");

String date = "Tue, Aug 16 2016";

LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate);

System.out.println(formatter.format(localDate));

}

}

Output

2016-08-16
Tue, Aug 16 2016

5. String = Tuesday, Aug 16, 2016 12:10:56 PM
This example convert a String to java.time.LocalDateTime

TestNewDate5.java
package com.mkyong.java8.date;

package com.mkyong.pageview;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TestNewDate5 {

public static void main(String[] argv) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a");

String date = "Tuesday, Aug 16, 2016 12:10:56 PM";

LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);

System.out.println(localDateTime);

System.out.println(formatter.format(localDateTime));

}

}

Output

2016-08-16T12:10:56
Tuesday, Aug 16, 2016 12:10:56 PM

6. String = 2016-08-16T15:23:01Z
The ‘Z’ suffix means UTC, you can convert into a java.time.instant directly, then display it with a time zone.

TestNewDate6.java
package com.mkyong.java8.date;

import java.time.*;

public class TestNewDate6 {

public static void main(String[] argv) {

String dateInString = "2016-08-16T15:23:01Z";

Instant instant = Instant.parse(dateInString);

System.out.println("Instant : " + instant);

//get date time only
LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));

//get localdate
System.out.println("LocalDate : " + result.toLocalDate());

//get date time + timezone
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime);

//get date time + timezone
ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens"));
System.out.println(zonedDateTime2);

}

}

Output

Instant : 2016-08-16T15:23:01Z
LocalDate : 2016-08-16
2016-08-17T00:23:01+09:00[Asia/Tokyo]
2016-08-16T18:23:01+03:00[Europe/Athens]

7. String = 2016-08-16T10:15:30+08:00
String -> ZonedDateTime -> LocalDate

TestNewDate7.java
package com.mkyong.java8.date;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class TestNewDate7 {

public static void main(String[] argv) {

String date = "2016-08-16T10:15:30+08:00";

ZonedDateTime result = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);

System.out.println("ZonedDateTime : " + result);

System.out.println("TimeZone : " + result.getZone());

LocalDate localDate = result.toLocalDate();

System.out.println("LocalDate : " + localDate);

}

}

Output

ZonedDateTime : 2016-08-16T10:15:30+08:00
TimeZone : +08:00
LocalDate : 2016-08-16
http://www.mkyong.com/java8/java-8-how-to-convert-string-to-localdate/
http://www.mkyong.com/tutorials/java-date-time-tutorials/

how-to-convert-string-to-localdate的更多相关文章

  1. How to convert String to Date – Java

    In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners ar ...

  2. svn: Can't convert string from 'UTF-8' to native encoding 的解决办法(转)

    http://www.cnblogs.com/xuxm2007/archive/2010/10/26/1861223.html svn 版本库中有文件是以中文字符命名的,在 Linux 下 check ...

  3. 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误

    转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...

  4. svn: Can't convert string from 'UTF-8' to native encoding 的解决办法

    http://www.leakon.com/archives/610 http://www.toplee.com/blog/566.html http://svnbook.red-bean.com/e ...

  5. SVN遇到Can't convert string from 'UTF-8' to native encoding

    刚配好mysql,svn co代码的时候遇到问题 svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/co ...

  6. Convert String to Long

    问题: Given a string, write a routine that converts the string to a long, without using the built in f ...

  7. How to convert string to wstring?

    How to convert string to wstring? - Codejie's C++ Space - C++博客     How to convert string to wstring ...

  8. svn: Can't convert string from 'UTF-8' to native

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt227 svn 版本库中有文件是以中文字符命名的,在 Linux 下 chec ...

  9. Java – How to convert String to Char Array

    Java – How to convert String to Char ArrayIn Java, you can use String.toCharArray() to convert a Str ...

  10. svn错误:Can't convert string from 'UTF-8' to native encoding

    如果文件名包含了中文,当执行"svn up ."遇到如下错误时: svn: Can't convert string from 'UTF-8' to native encoding ...

随机推荐

  1. 【医疗行业】关于dcm4che DICOM Toolkit:C-Move与C-Get

    在医学影像领域,DICOM标准与dcm4che的重要性毋庸置疑.幸好,这些开源的dcm4che DICOM 工具包,能对我们提供不少帮助. 在这里有很多的工具: Sources: https://gi ...

  2. google protocol buffer 简介 版本 安装 使用 实例

    一.简介 protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了三种语言的实现:java.c++ 和 python,每一种实现 ...

  3. 在hadoop上运行java文件

    hadoop 2.x版本 编译:javac -d . -classpath /usr/lib/hadoop/hadoop-common-2.2.0.2.0.6.0-102.jar TestGetPat ...

  4. Zabbix,Nagios,OneAPM Servers 安装部署大比拼

    怎样高速实现对 Linux server的监控? 做过server监控的开发人员差点儿都知道 Zabbix 和 Nagios ,他们都是提供系统监控以及网络监控功能的开源解决方式.资历比較老.在不久前 ...

  5. HTML5开发之获取设备的地理坐标

    近期一段时间開始学一下html5,发现里面有好多的知识还是蛮新颖的.所以整理了一下自己练习过的demo给大家分享下,以下的代码是通过js接口获取当前的地理坐标. <!doctype html&g ...

  6. Java DES 加解密("DES/EBC/NoPadding")

    private static final Cipher DES_CIPHER; static { try { DES_CIPHER = Cipher.getInstance("DES/ECB ...

  7. iOS 9 时代,iOS 7 占比接近 10% 该何去何从?

    iOS 9 时代.iOS 7 占比接近 10% 该何去何从? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&qu ...

  8. SpringMVC 参数中接收数组、List写法

    本文使用SpringMVC版本: org.springframework:spring-web:4.3.9.RELEASE 写法及说明(示例代码的类上的注解是@RestController,所以不需要 ...

  9. Spring Cloud开发实践 - 02 - Eureka服务和接口定义

    服务注册 EurekaServer Eureka服务模块只有三个文件, 分别是pom.xml, application.yml 和 EurekaServerApplication.java, 内容如下 ...

  10. Huginn部署到 Heroku

    折腾了几个小时,现在记录下安装步骤 1.本机Win10,放弃本地安装,官网建议也是, 2.安装虚拟机,装ubuntu系统 3.更换国内源(包括apt和gem,bunlde)ruby中国 4.注册her ...