1

大家好,

我试图通过使用 Calendar java 创建一个日期变量,以下是我的示例代码:

long day = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("MMM"); // not sure how to assign this value inside
cal.setTimeInMillis(day);
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 000);
Date todayDate = cal.getTime();

Timestamp current = new Timestamp(todayDate.getTime());

但是,current我得到的价值是2014-01-13 00:00:00.0. 我更喜欢将其设置01Jan

对此有什么想法吗?

4

5 回答 5

3

你的意思是这样的吗?

        long day = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S"); // not sure how to assign this value inside
        cal.setTimeInMillis(day);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 000);
        Date todayDate = cal.getTime();

        Timestamp current = new Timestamp(todayDate.getTime());
        System.out.println(month.format(current));

产量:

2014-1-13 00:00:00:0

请查看SimpleDateFormat更多格式选项。

于 2014-01-13T10:08:52.143 回答
1

你可以试试这个

    long day = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat month = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S");
    cal.setTimeInMillis(day);     
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 00);
    cal.set(Calendar.MILLISECOND, 000);
    Date todayDate = cal.getTime();

    Timestamp current = new Timestamp(todayDate.getTime());
    System.out.println(month.format(current));

现在输出:

    2014-Jan-13 00:00:00:0

ideone

于 2014-01-13T10:10:35.787 回答
0

// 不确定如何在里面分配这个值

你去吧.. ;)

yy = year (2 digit)
yyyy = year (4 digit)
M = month of the year
MM = month of the year (leading 0)
MMM = month of the year (short text)
MMMM = month of the year (full text)
d = day of the month
dd = day of the month (leading 0)
h = hour (1-12)
H = hour (0-23)
m = minute of the hour
s = second of the minute
S = milisecond
E = day of the week (short)
EEEE = day of the week (full)
D = day of the Year

SimpleDateFormat 类的附加链接

于 2014-01-13T10:20:00.983 回答
0

由于您正在寻找当前日期,因此您不需要currentTimeMillis().

相反,您可以执行以下操作来获得所需的输出。

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.S");
System.out.println(sdf.format(cal.getTime()));
于 2014-01-13T10:50:28.753 回答
0
于 2018-04-19T04:24:12.313 回答