how-to-convert-string-to-localdate
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的更多相关文章
- 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 ...
- 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 ...
- 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误
转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...
- 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 ...
- 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 ...
- Convert String to Long
问题: Given a string, write a routine that converts the string to a long, without using the built in f ...
- How to convert string to wstring?
How to convert string to wstring? - Codejie's C++ Space - C++博客 How to convert string to wstring ...
- svn: Can't convert string from 'UTF-8' to native
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt227 svn 版本库中有文件是以中文字符命名的,在 Linux 下 chec ...
- 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 ...
- svn错误:Can't convert string from 'UTF-8' to native encoding
如果文件名包含了中文,当执行"svn up ."遇到如下错误时: svn: Can't convert string from 'UTF-8' to native encoding ...
随机推荐
- JS密码校验规则前台验证(不能连续字符(如123、abc)连续3位或3位以上)(不能相同字符(如111、aaa)连续3位或3位以上)
密码必须为8到16位且必须包含数字和字母 密码必须包含特殊字符[_&#%] 不能连续字符(如123.abc)连续3位或3位以上 不能相同字符(如111.aaa)连续3位或3位以上 /** * ...
- 利用JQuery 来操作 ListBox和ListBox内移动
[导读]利用jquery 来操作 listbox和listbox内移动function listbox_move(listfrom,listto) { var size = $(" &quo ...
- 浅谈Spring的PropertyPlaceholderConfigurer
大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties的文件中,然后在我们系统初始化的时候,系统自动读取cfg.properties配置文 ...
- java第四节 类的继承/抽象/接口/多态性
/* 类的继承 类的继承可以简化类的定义 java只支持单继承,不允许多重继承 可以有多层继承,即一个类可以继承其一个类的子类,如类B继承了类A,类C又可以继承类B 那么类C也间接继承了类A 子类继承 ...
- 输出前 k 大的数
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 给定一个数组,统计前k大的数并且把这k个数从大到小输出. 输入 第一行包含一个整数n,表示数组的大小 ...
- VS 2013 with update 5 编译程序出现A task was cancel
打开进程管理器看,有一堆MSBuild.exe的实例,于是用 Taskkill /IM MSBuild.exe /F 杀死了所有实例.然后再重新打开VS2013,就work了. references: ...
- Git/Github的使用以及与Eclipse的整合
Git简介 Git是一个免费的.分布式的版本控制工具,或是一个强调了速度快的源代码管理工具.每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络 ...
- 邮件相关协议及JavaMail 包简介
1. 邮件服务器 按功能划分,邮件服务器可以划分为两种类型: SMTP邮件服务器:用于替用户发送邮件和接收外面发送给本地用户的邮件,相当于现实生活中邮局的邮件接收部门(可接收普通用户要投出的邮件和其他 ...
- Mac系统清理、占用空间大、空间不够、查看系统文件大小分布
背景: 最近老提示空间不够,很尴尬,一直弹系统提示 如图,256的空间,就剩下几个G了,其中最大头的系统占用:160G,占比60%多 正常情况下:我们可以点击管理,进入到系统磁盘优化界面: 这种适用于 ...
- Java中存储金额用什么数据类型?
转自:https://blog.csdn.net/u011277123/article/details/70214630 很早之前, 记得一次面试, 面试官问存储金钱用什么数据类型? 当时只知道8种数 ...