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. 通过adb命令在Android设备中执行Java命令, 并调用so文件。

    一.难点一:无法复制so文件到/system/lib或者/vendor/lib下,提示只读 解决方法: 2.使用android device monitor放库进入到 /system/lib出现只读权 ...

  2. Oracle 定时任务使用

    1:首先创建存储过程 create or replace procedure pro_rqsl_hmd is rsCursor sys_refcursor; rqslid ); nsrsbh ); h ...

  3. 关于IOS某图片添加控件,图片从相册或拍照保存后,再次进入时点击放大图无法显示的问题

    某图片添加控件: https://github.com/XZTLLQ/LQPhotoPickerDemo 问题: 标题已说明 代码块: NSArray *alAssetUrl =(NSMutableA ...

  4. Android 内存泄漏总结(转)

    Android 内存泄漏总结 内存管理的目的就是让我们在开发中怎么有效的避免我们的应用出现内存泄漏的问题.内存泄漏大家都不陌生了,简单粗俗的讲,就是该被释放的对象没有释放,一直被某个或某些实例所持有却 ...

  5. AndroidAnnotations库的使用

    AndroidAnnotations(Code Diet) android高速开发框架简单介绍: 项目地址:https://github.com/excilys/androidannotations ...

  6. VTK中模型的旋转与平移

    当从外界读入STL等三维模型时,其会按照它内部的坐标位置进行显示.因此它的位置和大小是确定的.但是在实际应用中,有可能需要人为地对这个模型在空间中进行旋转.平移或缩放等操作.VTK中有许多和旋转.平移 ...

  7. 51单片机——My-Clock项目

    技术:51单片机.光敏传感器.PCF8591.DHT11.DS1302.OLED显示屏   概述 项目My-Clock是一个环境监测时钟,接入光敏传感器和温湿度传感器监测环境信息,加入DS1302模块 ...

  8. 【note】EtherCAT Configurator 使用之主菜单介绍

    EtherCAT配置器是一个工具,用于配置EtherCAT总线. 工具管理一个或多个EtherCAT主设备和连接到该主站的EtherCAT从站.配置的设备能够脱机或能够通过扫描EtherCAT现场总线 ...

  9. 航信电子发票开发(servlet请求方式)

    在系统用户交费后,需要打印发票,可以选择普票或者机打票(票据信息在系统中自定义设置的),也可以打印电子发票,这里对接的是航信的电子发票,请求方式非web服务,而是使用servlet通过HTTP请求的方 ...

  10. <转>浅谈 Boost.Asio 的多线程模型

    本文转自:http://senlinzhan.github.io/2017/09/17/boost-asio/ Boost.Asio 有两种支持多线程的方式,第一种方式比较简单:在多线程的场景下,每个 ...