简单的java日期戳问题

发布于 2024-10-21 01:51:10 字数 474 浏览 7 评论 0原文

我希望捕获交易变量日期的当前日期。

这就是我正在寻找的格式。

我知道 simpleDateFormat 也适用于我的数据库。

这是我之前在 gui 中输入日期时用来输入日期的内容:

SimpleDateFormat sdf = new SimpleDateFormat("mm-dd-yyyy");
        try {
            ev.setDate(sdf.parse("12-10-2011"));
        } catch (ParseException ex) {
            Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
        }

我只是希望能够获取当前日期,而不必像上面的代码那样对字符串进行硬编码。

谢谢,

I am looking to capture the current date for a date of transaction variable.

this is the format I'm looking for.

I know simpleDateFormat works well with my db too.

here's what I've used to input the date before when it was a date that I typed in the gui:

SimpleDateFormat sdf = new SimpleDateFormat("mm-dd-yyyy");
        try {
            ev.setDate(sdf.parse("12-10-2011"));
        } catch (ParseException ex) {
            Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
        }

I just want to be able to get the current date instead of having to hard code a string like the above code.

thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2024-10-28 01:51:10

应该是

MM-dd-yyyy

编辑

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(sdf.format(new Date()));

should be

MM-dd-yyyy

Edit

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(sdf.format(new Date()));
客…行舟 2024-10-28 01:51:10

请使用以下代码:

SimpleDateFormat sdf = new SimpleDateFormet("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(new Date());

下面是初始化 sdf 对象时可能的值列表,该对象取自 此处

Letter  Date or Time Component  Presentation        Examples
G       Era designator          Text                AD
y       Year                    Year                1996; 96
M       Month in year           Month               July; Jul; 07
w       Week in year            Number              27
W       Week in month           Number              2
D       Day in year             Number              189
d       Day in month            Number              10
F       Day of week in month    Number              2
E       Day in week             Text                Tuesday; Tue
a       Am/pm marker            Text                PM
H       Hour in day (0-23)      Number              0
k       Hour in day (1-24)      Number              24
K       Hour in am/pm (0-11)    Number              0
h       Hour in am/pm (1-12)    Number              12
m       Minute in hour          Number              30
s       Second in minute        Number              55
S       Millisecond             Number              978
z       Time zone               General time zone   Pacific Standard Time; PST; GMT-08:00
Z       Time zone               RFC 822 time zone   -0800

Plz use the below code:

SimpleDateFormat sdf = new SimpleDateFormet("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(new Date());

Below is a list of possible values while initializing the sdf object taken from here:

Letter  Date or Time Component  Presentation        Examples
G       Era designator          Text                AD
y       Year                    Year                1996; 96
M       Month in year           Month               July; Jul; 07
w       Week in year            Number              27
W       Week in month           Number              2
D       Day in year             Number              189
d       Day in month            Number              10
F       Day of week in month    Number              2
E       Day in week             Text                Tuesday; Tue
a       Am/pm marker            Text                PM
H       Hour in day (0-23)      Number              0
k       Hour in day (1-24)      Number              24
K       Hour in am/pm (0-11)    Number              0
h       Hour in am/pm (1-12)    Number              12
m       Minute in hour          Number              30
s       Second in minute        Number              55
S       Millisecond             Number              978
z       Time zone               General time zone   Pacific Standard Time; PST; GMT-08:00
Z       Time zone               RFC 822 time zone   -0800
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文