GregorianCalendar输出:日期是java.util.GregorianCalendar [time = 1141556400000,areFieldsSet = true,areAllFieldsSet = true

时间:2015-05-25 08:45:44

标签: java type-conversion gregorian-calendar

我在文本文件中有以下数据,稍后我会在Scanner中使用。

17-Mar-2006 1100 1280
16-Jan-2002 1120 1230
15-Jun-2003 1140 1900

我正在将此数据构建为“Stockmarket”对象。 实例变量“date”声明为GregorianCalendar。 构造函数String dt应该在String中使用“dd-MMM-yyyy”形式。 同样适用于公共String date(),它应该返回一个“dd-MMM-yyyy”形式的字符串。 我是按照以下方式完成的,但是输出: 日期是java.util.GregorianCalendar [time = 1141556400000,areFieldsSet = true,areAllFieldsSet = true,lenient = true,zone = sun.util.calendar.ZoneInfo [id =“.....

有关修复的想法吗?

public class Stockmarket{
    // instance variables
    private GregorianCalendar date; 
    private double opening;
    private double closing; 

    public Stockmarket(String dt, double opening, double closing)

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
    Date d = sdf.parse(dt);
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    cal.setTime(d);
    this.date=cal;
    this.opening=opening;
    this.closing=closing;
    }  

    public String date(){

    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy, Locale.US");
    df.setCalendar(this.date);
    String date = df.format(this.date.getTime());
    return date;
    }

1 个答案:

答案 0 :(得分:1)

如果格式已知并且均匀,您可以使用SimpleDateFormat实现您想要的效果。

public class TestParse {
    public static void main(String[] args) {
        String myDate = "17-Mar-2006";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        Date date = null;
        try {
            date = simpleDateFormat.parse(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar calendar =  new GregorianCalendar();
        calendar.setTime(date);
        System.out.println(calendar.getTime());
    }
}

修改 根据问题变更添加了更多信息

方法getStringFromDate生成格式化日期 17-Mar-2006

public class TestParse {
    public static void main(String[] args) {
        String myDate = "17-Mar-2006";
        Date date = getDateFromString(myDate);
        // Set the Calendar
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // format date
        String formattedDate = getStringFromDate(calendar);
        System.out.printf("The formatted date is : %s & the dates are equal %s%n", formattedDate, formattedDate.equals(myDate));
    }

    public static String getStringFromDate(GregorianCalendar calendar) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
        simpleDateFormat.setCalendar(calendar);
        return simpleDateFormat.format(calendar.getTime());
    }

    public static Date getDateFromString(String input) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
        Date date = null;
        try {
            date = simpleDateFormat.parse(input);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

您的Stockmarket类格式固定类型

    public class Stockmarket {
        // instance variables
        private GregorianCalendar date;
        private double opening;
        private double closing;

        public Stockmarket(String dt, double opening, double closing) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
            Date d = null;
            try {
                d = sdf.parse(dt);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
            cal.setTime(d);
            this.date = cal;
            this.opening = opening;
            this.closing = closing;
        }

        public String date() {
            SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
            df.setCalendar(this.date);
            String date = df.format(this.date.getTime());
            return date;
        }
    }

测试类

public class TestParse {

    public void doTest()
    {
        Stockmarket stockmarket1 = new Stockmarket("17-Mar-2006",500,600);
        stockmarket1.date();
        System.out.println(stockmarket1.date());

        Stockmarket stockmarket2 = new Stockmarket("16-Jan-2002",500,600);
        stockmarket2.date();
        System.out.println(stockmarket2.date());

        Stockmarket stockmarket3 = new Stockmarket("15-Jun-2003",500,600);
        stockmarket3.date();
        System.out.println(stockmarket3.date());

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

        TestParse testParse = new TestParse();
        testParse.doTest();
    }
}