我试图在exp中存储到期日期。我想要存储它的格式是“yyyy-MM-dd”。我尝试使用SimpleDateFormat,然后尝试格式化以获取字符串,然后按如下方式解析字符串:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date exp = sdf.parse(sdf.format(c.getTime()));
但结果是我没有在exp中保存日期作为yyyy-MM-dd。 我想知道给定特定格式的日期如果输入本身是一种格式的日期并且输出日期是其他格式,如何将其转换为其他日期格式。我使用GregorianCalendar来获取时间。
生产日期格式为“yyyy-MM-dd”; 我希望日期以相同的格式存储在exp;
中public void calculateExpiryDate(List <Item> items) {
Iterator <Item> itr = items.iterator();
while (itr.hasNext()) {
Item i = itr.next();
Date md = i.getManufacturingDate();
int ubm = i.getUseBeforeMonths();
Calendar c = new GregorianCalendar();
c.setTime(md);
//System.out.println(c);
c.add(Calendar.MONTH, ubm);
Date exp = c.getTime();
i.setExpiryDate(exp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(md + " " + ubm + " " + sdf.format(exp) + " " + " " + i.getId());
}
}
这是我的项目代码:
public class Item implements Comparable<Item>{
private int id;
private String description;
private float weight;
private float price;
private Date manufacturingDate;
private int useBeforeMonths;
private Date expiryDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Date getManufacturingDate() {
return manufacturingDate;
}
public void setManufacturingDate(Date manufacturingDate) {
this.manufacturingDate = manufacturingDate;
}
public int getUseBeforeMonths() {
return useBeforeMonths;
}
public void setUseBeforeMonths(int useBeforeMonths) {
this.useBeforeMonths = useBeforeMonths;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
public Date getExpiryDate() {
return expiryDate;
}
@Override
public int compareTo(Item arg0)
{
Date exp1=getExpiryDate();
Date exp2=arg0.getExpiryDate();
if(exp1.getYear()==exp2.getYear())
{
if(exp1.getMonth()==exp2.getMonth())
{
return exp2.getDate()-exp1.getDate();
}
else
return exp2.getMonth()-exp1.getMonth();
}
return exp2.getYear()-exp1.getYear();
}
}
答案 0 :(得分:3)
如果我理解正确......
您希望在setExpiryDate
内将date
作为String
加入格式"yyyy-MM-dd"
。作为约束,Item.setExpiryDate
并不想关心任何SimpleDateFormat
。
解决方案是使用一种decorator pattern来重新定义日期的字符串表示。
public final class ExpirationDate extends Date {
private final SimpleDateFormat formatter;
public ExpirationDate(Date origin) {
super(origin.getTime());
this.formatter = new SimpleDateFormat("yyyy-MM-dd");
}
@Override
public final String toString() {
return formatter.format(this);
}
}
每当您需要此类表示时,将Date
包裹在ExpirationDate
中:
Date exp = c.getTime();
i.setExpiryDate(new ExpirationDate(exp));
答案 1 :(得分:0)
试试这个 -
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;
public class Temp {
public static void main(String args[]) {
String startDateString = "2012-03-12";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date startDate;
try{
startDate = df.parse(startDateString);
Calendar c= new GregorianCalendar();
c.setTime(startDate);
c.add(Calendar.MONTH, 3);
Date exp=c.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String expDate = sdf.format(exp).toString();
System.out.println(expDate);
}catch(Exception e){
}
}//Body
}
答案 2 :(得分:0)
我不确定您的生产日期是否以您提到的SDF格式显示。 SDF格式可用于以字符串格式存储日期
Calendar c = Calendar.getInstance();
Date md = new Date();
int ubm = 4;
//System.out.println(c);
c.setTime(md);
c.add(Calendar.MONTH, ubm);
Date exp = c.getTime();
//i.setExpiryDate(exp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strExpiry = sdf.format(exp);
System.out.println("Expiry Date in String format " + strExpiry );
Date dtExpiry = sdf.parse(strExpiry);
System.out.println("Expiry Date in Date format " + dtExpiry);
如果转换为Date,结果将再次是完整的Date输出,如下所示
字符串格式的到期日2016-02-01
截止日期为日期格式Mon Feb 01 00:00:00 IST 2016