java中怎样将字符串转化为date_如何将java字符串转换为Date对象

“ mm”表示日期的“分钟”片段。对于“月份”部分,请使用“ MM”。

因此,尝试将代码更改为:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

Date startDate = df.parse(startDateString);

编辑:DateFormat对象包含一个日期格式定义,而不是Date对象,它仅包含日期而无需考虑格式。在谈论格式时,我们正在谈论以特定格式创建日期的字符串表示形式。请参阅以下示例:

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws Exception {

String startDateString = "06/27/2007";

// This object can interpret strings representing dates in the format MM/dd/yyyy

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

// Convert from String to Date

Date startDate = df.parse(startDateString);

// Print the date, with the default formatting.

// Here, the important thing to note is that the parts of the date

// were correctly interpreted, such as day, month, year etc.

System.out.println("Date, with the default formatting: " + startDate);

// Once converted to a Date object, you can convert

// back to a String using any desired format.

String startDateString1 = df.format(startDate);

System.out.println("Date in format MM/dd/yyyy: " + startDateString1);

// Converting to String again, using an alternative format

DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");

String startDateString2 = df2.format(startDate);

System.out.println("Date in format dd/MM/yyyy: " + startDateString2);

}

}

输出:

Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007

Date in format MM/dd/yyyy: 06/27/2007

Date in format dd/MM/yyyy: 27/06/2007

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值