简体   繁体   English

如何以自定义日期格式返回Calendar对象

[英]How to return Calendar object in Custom Date format

How to return calender object using this. 如何使用此方法返回日历对象。

public static Calendar getDeCal(Timestamp timeStamp) {
    Calendar tempCal = new GregorianCalendar();
    tempCal.setTime(new Date(timeStamp.getTime()));
    return tempCal;
}

Its giving out put in this format without milliseconds. 它以毫秒为单位以这种格式发布。

2014-06-25T21:34:04+05:30

But we need in this format with milliseconds 但是我们需要这种格式以毫秒为单位

2014-06-25T21:34:04 .555+05:30

As per above requirement i have changed the code to this format 根据上述要求,我已将代码更改为此格式

public static Calendar getDateCal(Timestamp timeStamp) throws ParseException {
    Calendar tempCal = new GregorianCalendar();
    SimpleDateFormat ft =  new SimpleDateFormat ("yyyy.MM.dd 'T' hh:mm:ss.SS  z");
    String date1 = ft.format(timeStamp.getTime());
    tempCal.setTime(ft.parse(date1));
    return tempCal;
}

But its giving error while running. 但是它在运行时给出错误。

Any help about this. 关于此的任何帮助。

I am getting this error while ruining in standalone application. 我在独立应用程序中崩溃时遇到此错误。

Exception in thread "main" java.lang.IllegalArgumentException
    at java.util.Date.parse(Unknown Source)
    at java.util.Date.<init>(Unknown Source)

If you want to have the Calendar object as a variable and print it, then use the SimpleDateFormat object to format it when you are ready to display the output. 如果要将Calendar对象作为变量并打印它,则在准备显示输出时,请使用SimpleDateFormat对象设置其格式。 I did not see any way to automatically fill the time zone colon through the format characters so I used a substring to add one instead. 我没有看到任何通过格式字符自动填充时区冒号的方法,因此我使用了一个子字符串来添加一个。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZZZ");

Calendar c = Calendar.getInstance();

String formattedTime = sdf.format(c.getTime());

int colon = formattedTime.length()-2;
formattedTime = formattedTime.substring(0,colon) + ":"
    + formattedTime.substring(colon);

System.out.println(formattedTime);

Output:2015-01-05T13:30:49. 输出:2015-01-05T13:30:49。 635-06:00 635-06:00

EDIT: If you are looking for a single object that is a calendar and can generate output in the given format, you would have to make a new class to connect the two together. 编辑:如果您正在寻找一个单独的对象,它是一个日历,并可以以给定的格式生成输出,则您必须制作一个新的类将两者连接在一起。 The output and the calendar object are two different things. 输出和日历对象是两个不同的东西。 The calendar represent the time. 日历代表时间。 The output represents the human readable portion, the string. 输出表示人类可读的部分,字符串。

public class PersonalCalendar extends GregorianCalendar
{
  SimpleDateFormat sdf;
  public PersonalCalendar(Timestamp timestamp)
  {
    super();
    setTime(timestamp);
    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZZZ");
  }

  public String toString()
  {
    String formattedTime = sdf.format(getTime());
    int colon = formattedTime.length()-2;
    formattedTime = formattedTime.substring(0,colon) + ":"+ formattedTime.substring(colon);
    return formattedTime;
  }
}

In use. 正在使用。

Calendar c = new PersonalCalendar(timestamp);
System.out.println(c.toString());

Output:2015-01-06T09:29:29. 输出:2015-01-06T09:29:29。 783-06:00 783-06:00

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

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