Java在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
sudoku solver (not completed) 发布于:2024-03-16 23:48 java第一次作业 发布于:2024-03-15 00:22 获取个十百 发布于:2024-03-14 23:38 MD5编码规则 与时间格式化 发布于:2024-03-13 14:18 传递继承的示例 发布于:2024-03-12 18:10 斐波那契数列 发布于:2024-03-12 17:36 DS 代码示例保存。 发布于:2024-03-06 08:47 测试功能运行 发布于:2024-03-04 17:18 写出一个完整的声明复数类的iava程序输入2个括号字符串如 (1,2) (34) 发布于:2024-03-03 20:16 这是数据科学与算法 发布于:2024-02-28 02:04 健康和健康很快就会健康和健康 发布于:2024-02-26 16:45 人员信息匹配 发布于:2024-02-24 18:00 构造方法使用 发布于:2024-02-24 16:25 方法的重载 发布于:2024-02-22 12:50 迷宫老鼠问题 发布于:2024-02-21 16:33 类与对象的使用 发布于:2024-02-21 15:32 撒地方大师傅 发布于:2024-02-19 11:16 我的测试代码 发布于:2024-02-19 10:19 java AES 加密 发布于:2024-02-18 16:47 插入元素排序 发布于:2024-02-18 12:42 打印杨辉三角形 发布于:2024-02-18 10:46 冒泡排序法 发布于:2024-02-17 22:28 为数组添加新元素 发布于:2024-02-17 18:21 数组的使用 发布于:2024-02-16 20:21 1-1/2..求和 发布于:2024-02-15 19:58 大小写字母 发布于:2024-02-15 19:45 输出不被5整除的数 发布于:2024-02-15 18:48 提示登陆成功 发布于:2024-02-15 17:21 打印空心金字塔 发布于:2024-02-15 15:52 乘法口诀表 发布于:2024-02-15 14:31 for循环的嵌套使用 发布于:2024-02-15 13:51 dowhile循环的使用 发布于:2024-02-15 13:21 for循环count 发布于:2024-02-15 11:55 switch判断成绩合格 发布于:2024-02-15 11:17 if else控制语句 发布于:2024-02-15 08:46 发布于:2024-02-14 21:51 Scanner扫描器 发布于:2024-02-14 18:40 java学习 发布于:2024-02-08 01:05 我的画图脚本 发布于:2024-02-06 22:07 小红书的签名算法 发布于:2024-02-06 18:08 输入任意数字,获取大于该数字的偶数 发布于:2024-02-06 10:24 java数字排序程序 发布于:2024-01-31 14:44 23544符合法规和国家与iu哦 发布于:2024-01-31 09:55 归并排序算法 发布于:2024-01-31 09:04 加密算法-SHA256 发布于:2024-01-30 14:27 温度转换程序 发布于:2024-01-31 14:40 插入排序算法 发布于:2024-01-25 22:01 数组里有两种数是奇数个,其他都是偶数个,要求时间复杂度O(n),空间复杂度O(n) 发布于:2024-01-25 21:52 冒泡排序算法 发布于:2024-01-25 09:27 选择排序算法 发布于:2024-01-25 09:18 [更多]
显示目录

Java 8 – How to convert String to LocalDate



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

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

References

  1. DateTimeFormatter JavaDoc
  2. Classic SimpleDateFormat JavaDoc
  3. Java – How to convert String to Date
  4. Stackoverflow : simpledateformat parsing date with ‘Z’ literal
  5. Wikipedia : ISO 8601
  6. GMT VS UTC
  7. What is a Time Zone?
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout