日历构造函数 Java toString

Calendar Constructor Java toString(日历构造函数 Java toString)
本文介绍了日历构造函数 Java toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

What I'm trying to do is pass a date into the Calendar so that it will format the date ready for use with another constructor. So that i can make use of it later using the functions that calendar has provides.

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 方法.

The tostring method.

public String toString(){
    String s = "nDate: " + DateD;
    return s;
}

日期:java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true

Date: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true

而不是日期:20/10/2012

Rather than Date: 20/10/2012

推荐答案

假设 DateDCalendar ,则默认为 toString() 实现.您需要调用 getTime() 来获取 date.

Assuming DateD is Calendar , it is default toString() implementation. You need to call getTime() to get the date out of it.

来自 日历#toString()

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

Return a string representation of this calendar. This method is intended to be used only for debugging purposes, and the format of the returned string may vary between implementations. The returned string may be empty but may not be null.

您可以使用 SimpleDateFormat 进行转换它到 String

You can use SimpleDateFormat to convert it to String

这篇关于日历构造函数 Java toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)