日历构造函数Java toString

日历构造函数Java toString,java,constructor,calendar,Java,Constructor,Calendar,我要做的是将日期传递到日历中,这样它就可以格式化日期,以便与另一个构造函数一起使用。这样我以后就可以使用calendar提供的功能来使用它了 public class Top { public static void main(String[] args) { Something st = new Something(getCalendar(20,10,2012)); System.out.println(st.toString()); }

我要做的是将日期传递到日历中,这样它就可以格式化日期,以便与另一个构造函数一起使用。这样我以后就可以使用calendar提供的功能来使用它了

public class Top {
public static void main(String[] args) {
    Something st = new Something(getCalendar(20,10,2012));       
    System.out.println(st.toString());       
    }

public static Calendar getCalendar(int day, int month, int year){
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, day);
    return cal;
    }
}
tostring方法

public String toString(){
    String s = "nDate: " + DateD;
    return s;
}
日期:java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true

而不是
日期:2012年10月20日

假设
日期
日历
,它是默认的
toString()
实现。您需要调用
getTime()
从中获取
日期

来自

返回此日历的字符串表示形式。此方法仅用于调试目的,返回字符串的格式可能因实现而异。返回的字符串可能为空,但不能为null


您可以使用将其转换为
String

首先,在打印实例时不需要显式使用
toString()
方法。它将被自动调用

String output = ld.toString() ;
此外,您还应使用将日期格式化为所需的字符串格式:-

Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String date = format.format(cal.getTime());

System.out.println(date);
输出:-

2012/10/20

如果要将日历实例表示的日期打印为字符串,则应使用以所需格式格式化日期,如下所示:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy");
System.out.println(sdf.format(DateD.getTime());

对我来说好像工作太多了

作为一个用户,我希望传递一个日期并明确合同。提供一个方便的方法将字符串转换为日期:

public class Top {

    public static final DateFormat DEFAULT_FORMAT;

    static {
        DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MMM-dd");
        DEFAULT_FORMAT.setLenient(false);
    }

    public static void main(String [] args) {
    }

    public static Date convert(String dateStr) throws ParseException {
        return DEFAULT_FORMAT.parse(dateStr);
    }     

    public static String convert(Date d) {
        return DEFAULT_FORMAT.format(d);
    }   
}
LocalDate
显然,您想要一个只包含日期的值,而不包含日期。为此,请使用
LocalDate
类,而不是
Calendar
Calendar
类是一个日期加上一天的时间。此外,
Calendar
现在是传统的,在被证明是麻烦的、混乱的、不确定的时间之后被java.time类取代nd有缺陷

只需将所需的年、月和月日传递给工厂方法即可。与
日历不同,一月至十二月的月份编号为1-12

LocalDate ld = LocalDate.of( 2012 , 10 , 20 );
或者,将常数传递一个月

LocalDate ld = LocalDate.of( 2012 , Month.OCTOBER , 20 );
time类倾向于使用静态工厂方法,而不是带有
new
的构造函数

串 要生成标准ISO 8601格式的字符串,请调用
toString

String output = ld.toString() ;
2012-10-20

对于其他格式,请在堆栈溢出中搜索
DateTimeFormatter
。例如:

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
String output = ld.format( f );
2012年10月20日


@Downvoter。好的,我改进了我的帖子。我想你现在可以删除Downvoter了。嗨,Rohit,我不喜欢字符串连接解决方案,但这是我个人的选择。我会反向选择。@duffymo。是的,谢谢:)。我知道我第一次做错了。
DateFormat
应该是格式化日期的第一选择。@duffymo。不过我喜欢你的方式。Tha当转换太多时,t太方便了:)SimpleDataFormat不是线程安全的,不应存储为静态字段。您需要根据需要创建本地SimpleDataFormat,或者查看线程安全的替代方案(Apache commons lang具有FastDateFormat),池,或使用ThreadLocal。谢谢你,这是我一直在寻找的解决方案。我会投票支持它,但这是我在这里遇到的第一个问题。仅供参考,
java.util.Calendar
是一个旧的日期时间类,它被证明是麻烦的、混乱的和有缺陷的。现在是遗留的,被类取代。请改用。