简体   繁体   English

从Calendar对象获取格式化的日期对象

[英]Get formatted date object from Calendar object

I have this code 我有这个代码

String date = "18/06/2014";

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Calendar c = Calendar.getInstance();

c.setTime(sdf.parse(date));

c.add(Calendar.DATE, +2); 

I'm trying to get formatted date object from calendar object but without any success. 我正在尝试从日历对象获取格式化的日期对象,但没有成功。 Do you have any suggestion? 你有什么建议吗?

Date is an object, it doesn't have an inherent textual representation. Date是一个对象,它没有固有的文本表示形式。 The SimpleDateFormat class can be used to provide the textual representation you're looking for, SimpleDateFormat类可用于提供您要查找的文本表示形式,

String date = "30/06/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, +2);
System.out.println(sdf.format(c.getTime()));

Will output 将输出

02/07/2014

Are you looking for this? 您在找这个吗?

Date date = sdf.parse(date); 日期date = sdf.parse(date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM