Java字符串转日期转换

1007

在Java中,将格式为“January 2, 2010” 的 String 转换为 Date 的最佳方法是什么?

最终,我想将月份、日期和年份分解为整数,以便可以使用。

Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();

把日期转换成时间。


16
请注意,许多答案忽略了地区和时区等细节。在使用任何这些类之前,建议仔细阅读Date、Calendar、TimeZone、Locale和SimpleDateFormat类的文档。 - Kristopher Johnson
2
可能是 将字符串转换为 java.util.Date 的重复问题。 - Ankur
7
Java 8提供了一个新的日期/时间API。如果您正在使用Java 8(或更新版本),您应该查看这个答案:https://dev59.com/SW855IYBdhLWcg3wp2N0#22180505 - micha
2
Date 的所有 setter 方法都已被弃用。 - Raedwald
7
请注意,诸如java.util.Datejava.util.Calendarjava.text.SimpleTextFormat等老旧的日期时间类现已被视为遗留系统,取而代之的是Java.time类。请参见Oracle的教程 - Basil Bourque
显示剩余2条评论
17个回答

5

这是我的一份简单测试程序。我用它来玩弄格式化器,并查找日志文件中的长日期(但是谁把它们放在那里了呢...)。

我的测试程序:

package be.test.package.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class TimeWork {

    public static void main(String[] args) {    

        TimeZone timezone = TimeZone.getTimeZone("UTC");

        List<Long> longs = new ArrayList<>();
        List<String> strings = new ArrayList<>();

        //Formatting a date needs a timezone - otherwise the date get formatted to your system time zone.
        //Use 24h format HH. In 12h format hh can be in range 0-11, which makes 12 overflow to 0.
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
        formatter.setTimeZone(timezone);

        Date now = new Date();

        //Test dates
        strings.add(formatter.format(now));
        strings.add("01-01-1970 00:00:00.000");
        strings.add("01-01-1970 00:00:01.000");
        strings.add("01-01-1970 00:01:00.000");
        strings.add("01-01-1970 01:00:00.000");
        strings.add("01-01-1970 10:00:00.000");
        strings.add("01-01-1970 12:00:00.000");
        strings.add("01-01-1970 24:00:00.000");
        strings.add("02-01-1970 00:00:00.000");
        strings.add("01-01-1971 00:00:00.000");
        strings.add("01-01-2014 00:00:00.000");
        strings.add("31-12-1969 23:59:59.000");
        strings.add("31-12-1969 23:59:00.000");
        strings.add("31-12-1969 23:00:00.000");

        //Test data
        longs.add(now.getTime());
        longs.add(-1L);
        longs.add(0L); //Long date presentation at - midnight 1/1/1970 UTC - The timezone is important!
        longs.add(1L);
        longs.add(1000L);
        longs.add(60000L);
        longs.add(3600000L);
        longs.add(36000000L);
        longs.add(43200000L);
        longs.add(86400000L);
        longs.add(31536000000L);
        longs.add(1388534400000L);
        longs.add(7260000L);
        longs.add(1417706084037L);
        longs.add(-7260000L);

        System.out.println("===== String to long =====");

        //Show the long value of the date
        for (String string: strings) {
            try {
                Date date = formatter.parse(string);
                System.out.println("Formated date : " + string + " = Long = " + date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        System.out.println("===== Long to String =====");

        //Show the date behind the long
        for (Long lo : longs) {
            Date date = new Date(lo);
            String string = formatter.format(date);
            System.out.println("Formated date : " + string + " = Long = " + lo);        
        }
    }
}

测试结果:

===== String to long =====
Formated date : 05-12-2014 10:17:34.873 = Long = 1417774654873
Formated date : 01-01-1970 00:00:00.000 = Long = 0
Formated date : 01-01-1970 00:00:01.000 = Long = 1000
Formated date : 01-01-1970 00:01:00.000 = Long = 60000
Formated date : 01-01-1970 01:00:00.000 = Long = 3600000
Formated date : 01-01-1970 10:00:00.000 = Long = 36000000
Formated date : 01-01-1970 12:00:00.000 = Long = 43200000
Formated date : 01-01-1970 24:00:00.000 = Long = 86400000
Formated date : 02-01-1970 00:00:00.000 = Long = 86400000
Formated date : 01-01-1971 00:00:00.000 = Long = 31536000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388534400000
Formated date : 31-12-1969 23:59:59.000 = Long = -1000
Formated date : 31-12-1969 23:59:00.000 = Long = -60000
Formated date : 31-12-1969 23:00:00.000 = Long = -3600000
===== Long to String =====
Formated date : 05-12-2014 10:17:34.873 = Long = 1417774654873
Formated date : 31-12-1969 23:59:59.999 = Long = -1
Formated date : 01-01-1970 00:00:00.000 = Long = 0
Formated date : 01-01-1970 00:00:00.001 = Long = 1
Formated date : 01-01-1970 00:00:01.000 = Long = 1000
Formated date : 01-01-1970 00:01:00.000 = Long = 60000
Formated date : 01-01-1970 01:00:00.000 = Long = 3600000
Formated date : 01-01-1970 10:00:00.000 = Long = 36000000
Formated date : 01-01-1970 12:00:00.000 = Long = 43200000
Formated date : 02-01-1970 00:00:00.000 = Long = 86400000
Formated date : 01-01-1971 00:00:00.000 = Long = 31536000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388534400000
Formated date : 01-01-1970 02:01:00.000 = Long = 7260000
Formated date : 04-12-2014 15:14:44.037 = Long = 1417706084037
Formated date : 31-12-1969 21:59:00.000 = Long = -7260000

2

我曾经处理过将字符串解析为LocalDateTime的任务。以下是一个例子:

LocalDateTime d = LocalDateTime.parse("20180805 101010", DateTimeFormatter.ofPattern("yyyyMMdd HHmmss"));

我得到了 这里输入图片的描述


2

源链接Link

对于Android

Calendar.getInstance().getTime() 返回

Thu Jul 26 15:54:13 GMT+05:30 2018

使用

String oldDate = "Thu Jul 26 15:54:13 GMT+05:30 2018";
DateFormat format = new SimpleDateFormat("EEE LLL dd HH:mm:ss Z yyyy");
Date updateLast = format.parse(oldDate);

1
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
return sdf.format(date);

SimpleDateFormat sdf = new SimpleDateFormat(datePattern); 
return sdf.parse(dateStr);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat newSdf = new SimpleDateFormat("dd-MM-yyyy");
Date temp = sdf.parse(dateStr);
return newSdf.format(temp);

来源 link


1
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  Date date1 = null;
  Date date2 = null;

  try {
    date1 = dateFormat.parse(t1);
    date2 = dateFormat.parse(t2);
  } catch (ParseException e) {
    e.printStackTrace();
  }
  DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
  String StDate = formatter.format(date1);
  String edDate = formatter.format(date2);

  System.out.println("ST  "+ StDate);
  System.out.println("ED "+ edDate);

不好意思,您是不是在重复已经过时且不明智的几个答案中所提到的那个臭名昭著、早已过时的 SimpleDateFormat 类及其相关类?我建议您不要使用 SimpleDateFormatDateFormatDate。相反,建议您使用 java.time,现代 Java 日期和时间 API 中的 LocalDateTimeDateTimeFormatter - Ole V.V.

0

字符串转日期:

private Date StringtoDate(String date) throws Exception {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            java.sql.Date sqlDate = null;
            if( !date.isEmpty()) {

                try {
                    java.util.Date normalDate = sdf1.parse(date);
                    sqlDate = new java.sql.Date(normalDate.getTime());
                } catch (ParseException e) {
                    throw new Exception("Not able to Parse the date", e);
                }
            }
            return sqlDate;
        }

-1

试试这个

String date = get_pump_data.getString("bond_end_date");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date datee = (Date)format.parse(date);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接