BlackBerry 的当前日期

发布于 2024-12-09 17:34:20 字数 311 浏览 0 评论 0原文

我正在开发一个需要设备当前日期的应用程序,但它只需要星期几、月份和月份,例如 10 月 14 日星期五

我已经尝试过此代码日历。如何将日期转换为字符串?是否可以获取这种格式的日期?

 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 System.out.println("Date" + c.getTime());

 and my output : `Fri Oct 14 16:17:03 Asia/Calcutta 2011`

I am developing an application which needs the current date from the device, but it only needs day-of-week, month, and day-of-month, like Friday October 14

I have tried this code with Calendar. How do I convert Date to String? Is this possible to get date in this format?

 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 System.out.println("Date" + c.getTime());

 and my output : `Fri Oct 14 16:17:03 Asia/Calcutta 2011`

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

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

发布评论

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

评论(1

过度放纵 2024-12-16 17:34:20

您可以使用 SimpleDateFormat

SimpleDateFormat fmt = new SimpleDateFormat("EEEE MMMM dd");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.print("Date ");
System.out.println(fmt.format(c.getTime()));

如果您需要不同字符串中的这些值,它会变得有点复杂(使用 DateFormatSymbols 到获取月份/工作日名称):

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormatSymbols dateSymbols = new DateFormatSymbols();
String[] monthsText = dateSymbols.getMonths();
String[] weekdaysText = dateSymbols.getWeekdays();

String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String month = monthsText[c.get(Calendar.MONTH)];
String weekday = weekdaysText[c.get(Calendar.DAY_OF_WEEK)];

System.out.format("day: %s, month: %s, weekday: %s", day, month, weekday);               

You could use SimpleDateFormat:

SimpleDateFormat fmt = new SimpleDateFormat("EEEE MMMM dd");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.print("Date ");
System.out.println(fmt.format(c.getTime()));

If you need those values in different Strings it gets a little bit more complicated (using DateFormatSymbols to get the month/weekday names):

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormatSymbols dateSymbols = new DateFormatSymbols();
String[] monthsText = dateSymbols.getMonths();
String[] weekdaysText = dateSymbols.getWeekdays();

String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String month = monthsText[c.get(Calendar.MONTH)];
String weekday = weekdaysText[c.get(Calendar.DAY_OF_WEEK)];

System.out.format("day: %s, month: %s, weekday: %s", day, month, weekday);               
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文