【问题标题】:Calendar Constructor Java toString日历构造函数 Java toString
【发布时间】:2012-10-20 14:21:39
【问题描述】:

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

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

而不是 日期:20/10/2012

【问题讨论】:

  • 仅供参考,java.util.Calendar 是旧的日期时间类之一,已被证明是麻烦、混乱和有缺陷的。现在是遗留的,被java.time 类所取代。请改用ZonedDateTime

标签: java constructor calendar


【解决方案1】:

假设 DateDCalendar ,它是默认的 toString() 实现。您需要致电getTime() 以获取date

来自Calendar#toString()的java文档

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

您可以使用SimpleDateFormat 将其转换为String

【讨论】:

  • 谢谢,这是我正在寻找的解决方案。我会投票,但这是我在这里的第一个问题。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    对我来说工作量太大了。

    作为用户,我宁愿传递一个日期并明确合同。提供一个将 String 转换为 Date 的便捷方法:

    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);
        }   
    }
    

    【讨论】:

    • SimpleDateFormat 不是线程安全的,不应存储为静态字段。您需要根据需要创建本地 SimpleDateFormat,或查看线程安全替代方案(Apache commons lang 有 FastDateFormat)、池化或使用 ThreadLocal。
    【解决方案4】:

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

    此外,您应该使用SimpleDateFormat 将您的Date 格式化为所需的字符串格式:-

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

    输出:-

    2012/10/20
    

    【讨论】:

    • @Downvoter。好的,我已经改进了我的帖子。我认为您现在可以删除反对票。
    • 嗨 Rohit,我不关心字符串连接解决方​​案,但这是个人选择。我会反转。
    • @duffymo。是的,谢谢:)。我知道我第一次做错了。DateFormat 应该是格式化日期的首选。
    • @duffymo。不过我喜欢你的方式。当转换太多时,这太方便了:)
    【解决方案5】:

    LocalDate

    显然您想要一个没有日期的仅日期值。为此使用LocalDate 类而不是CalendarCalendar 类用于日期和时间。此外,Calendar 现在是遗留的,在被证明是麻烦、混乱和有缺陷之后被 java.time 类所取代。

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

    LocalDate ld = LocalDate.of( 2012 , 10 , 20 );
    

    或者,传递一个月的常数。

    LocalDate ld = LocalDate.of( 2012 , Month.OCTOBER , 20 );
    

    java.time 类倾向于使用静态工厂方法而不是带有new 的构造函数。

    字符串

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

    String output = ld.toString() ;
    

    2012-10-20

    对于其他格式,请在 Stack Overflow 中搜索 DateTimeFormatter。例如:

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
    String output = ld.format( f );
    

    2012 年 10 月 20 日

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2018-12-18
      • 1970-01-01
      相关资源
      最近更新 更多