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. openfiler在esxi下的安装配置

    注意分区的时候如果硬盘太小自动分区会导致分配的卷大小不够用 后改为如下: 以root登录: 应该以openfiler登录,口令是password 也可以导入虚拟机安装 升级虚拟机硬件版本 终端登录用户 ...

  2. 树莓派3中运行Netcore2.0程序

    一.简介 Netcore2.0发部后,可以运行在Arm平台上.因此,我们可以尝试在装了Debain的树莓派中运行. 二.方法: 1.在自己的电脑上使用VS写一个NetCore2.0的控制台程序,我假设 ...

  3. Go语言中Socket通信之Tcp客户端

    1.用法: (1)定义远程IP地址.使用net.ResolveTCPAddr()方法,定义一个TCP地址,做为目标连接地址. (2)调用net.DialTCP("tcp",nil, ...

  4. Android Webservices 返回多行多列数据(Dataset)

    对于之前从事.net或者java开发人员,习惯了从后台获取网格数据(多行多列DataTable),但转行从事android开发,难免会不习惯 Android调用Webservice时,如果返回值是一个 ...

  5. Android StageFrightMediaScanner源码解析

    1. 简单介绍 Android中在StageFrightMediaScanner实现对多媒体文件的处理. 此外在StageFrightMediaScanner定义了支持的多媒体文件类型. 文件位置 f ...

  6. SpringMVC Controller配置方法有哪几种

    第一种 URL对应Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 表示将请求的URL和Bean名字映射--> <bean class="org.s ...

  7. 前台登录和Token信息交互流程

    原来总是对前台登录,怎么利用token有点迷惑,后面仔细的想了一遍,把自己简单的想法记录下来,留作记录,以便后续优化 各路大神有什么看法也可以说,能更完善整个流程. 不说了,暴力的上图: 该图是出自c ...

  8. infobright系列二:数据迁移

    安装之后把之前infobright的数据迁移到新安装的infobright上. 1:挺掉相关的服务 2:scp 把旧数据拷到新安装的infobright上 3:修改/etc/my-ib.cnf的数据目 ...

  9. emplace_back() 和 push_back 的区别(转)

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  10. VBA 一个很神奇的东西

    百度经验参考:http://jingyan.baidu.com/article/4ae03de32663953efe9e6b47.html 今天奇迹般的发现了VBA,都怪自己平时使用excle不够多, ...