集册 Java实例教程 使用SimpleDateFormat将日期字符串从一种格式转换为另一种格式

使用SimpleDateFormat将日期字符串从一种格式转换为另一种格式

欢马劈雪     最近更新时间:2020-01-02 10:19:05

515
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用SimpleDateFormat将日期字符串从一种格式转换为另一种格式

import java.util.Date;/**来自 N o w  J a v a  .   c o m**/

import java.text.ParseException;

import java.text.SimpleDateFormat;

 

public class Main {

 

  public static void main(String[] args) {

    String strDate = "12/12/18";

   

    try

    {

      SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy");

     

      Date date = sdfSource.parse(strDate);

     

      SimpleDateFormat sdfDestination = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");

     

      strDate = sdfDestination.format(date);/*N o w J a v a . c o m - 时  代  Java 提 供*/

     

      System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");

      System.out.println(
展开阅读全文