Tag: dayofweek

将字符串转换为星期几(不是确切的日期)

我收到一个String ,这是一周的拼写日,例如星期一。 现在我想获得当天的常量整数表示,它在java.util.Calendar 。 我是否真的必须做if(day.equalsIgnoreCase(“Monday”)){…}else if(…){…}我自己? 有一些简洁的方法吗? 如果我挖掘SimpleDateFormat并将其与Calendar混合,我生成的行几乎与键入丑陋的if-else-to-infitity statetment一样多。

从字符串开始的Java星期几

我有这个简单的代码: SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”); Date date = format.parse(“2011-10-29”); calendar.setTime(date); Log.d(“Debug”,”Day of the week = “+(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)); 十月二十九日是星期六,为什么我会弄错?

DateTimeFormatter基于周的年份差异

我正在将我的应用程序从Joda-Time迁移到Java 8 java.time 。 我遇到的一件事是使用DateTimeFormatter的模式打印基于周的年份。 注意:我已经看到了这个问题: Java Time使用DateTimeFormatter解析基于星期的周模式 根据文件 y year-of-era year 2004; 04 Y week-based-year year 1996; 96 然而,当我尝试这两个时,似乎Y总是和y一样返回。 我的测试代码: DateTimeFormatter yearF = DateTimeFormatter.ofPattern(“yyyy”).withZone(ZoneOffset.UTC); DateTimeFormatter weekYearF = DateTimeFormatter.ofPattern(“YYYY”).withZone(ZoneOffset.UTC); DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR_OF_ERA) .appendLiteral(” “) .append(yearF) .appendLiteral(” — “) .appendValue(IsoFields.WEEK_BASED_YEAR) .appendLiteral(” “) .append(weekYearF) .toFormatter() .withZone(ZoneOffset.UTC); System.out.println(dateTimeFormatter.toString()); ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(946778645000L), ZoneOffset.UTC); for (int […]

用Java获取当前的开始和结束日期 – (周一至周日)

今天是2014-04-06(星期日)。 我使用以下代码得到的输出是: Start Date = 2014-04-07 End Date = 2014-04-13 这是我想要的输出: Start Date = 2014-03-31 End Date = 2014-04-06 我怎样才能做到这一点? 这是我到目前为止完成的代码: // Get calendar set to current date and time Calendar c = GregorianCalendar.getInstance(); System.out.println(“Current week = ” + Calendar.DAY_OF_WEEK); // Set the calendar to monday of the current week c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(“Current week = […]

如何在Java中获取给定周数的第一天

让我解释一下自己。 通过了解周数和日期年份: Date curr = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(curr); int nweek = cal.WEEK_OF_YEAR; int year = cal.YEAR; 但现在我不知道如何获得该周第一天的日期。 我一直在寻找日历,日期,日期格式,但没有什么可能有用…… 有什么建议吗? (在Java工作)

检索当前周的周一日期

我们有一个实用工具,将在周一至周五之间的任何一天运行。 它将更新内容管理工具中的一些文件。 与该文件关联的最后修改日期应该是该周的星期一日期。 我编写了以下程序来检索当前周的星期一日期。 但我仍然不确定这是否适用于所有情况。 有没有人有更好的解决方案? Calendar c = Calendar.getInstance(); c.setTime(new Date()); System.out.println(c.get(Calendar.DAY_OF_MONTH)); System.out.println(c.get(Calendar.DAY_OF_WEEK)); int mondayNo = c.get(Calendar.DAY_OF_MONTH)-c.get(Calendar.DAY_OF_WEEK)+2; c.set(Calendar.DAY_OF_MONTH,mondayNo); System.out.println(“Date “+c.getTime());