如何在H2

时间:2017-01-09 06:54:28

标签: java date timestamp h2 week-number

我想要一个带有周数和年份的日期格式,其格式为yyyy - ' W' ww。但是,由于H2数据库中只有周功能,我只能得到没有年份的周数。如何以这样的方式格式化我添加默认年份。 2016年的事情 - ' W' ww。

目前,我正在使用此功能(这绝对不是正确的方法)

  

周(PARSEDATETIME(TRUNC(" + this.fieldName +"),' 2016ww')   周(PARSEDATETIME(TRUNC(" + this.fieldName +"),' 2016-ww')

我无法得到其他可以做的事情。任何人都可以帮助我

2 个答案:

答案 0 :(得分:1)

不同的库中有几种有效的解决方案。但是,您需要知道使用标准日历年“y”是错误的。相反,你必须使用星期日(或称为基于周的年份),并带有符号“Y”(大写字母)。

使用旧版<Page ... xmlns:converter="using:VisibilyConverter" ... > <Grid x:Name="LayoutRoot"> <Grid.Resources> <converter:VisibiliyConverter x:Key="visibilityConverter" /> </Grid.Resources> <ListBox> <ListBox.ItemTemplate> <DataTemplate> <Grid Grid.Row="1" HorizontalAlignment="Right" Margin="13,0,13,13" Visibility="{Bindig IsSelected, Mode=OneWay, Converter={StaticResource visibilityConverter}}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <HyperlinkButton x:Name="SendButton" VerticalAlignment="Center" IsTabStop="True" TabIndex="3" Margin="7,0,0,0" Content="Send" Style="{StaticResource HyperlinksStyle}" Click="SendButtonClick /> <HyperlinkButton x:Name="EditButton" VerticalAlignment="Center" IsTabStop="True" TabIndex="3" Grid.Column="1" Margin="7,0,0,0" Content="Edit" Style="{StaticResource HyperlinksStyle}" Click="EditButtonClick /> <HyperlinkButton x:Name="DeleteButton" VerticalAlignment="Center" IsTabStop="True" TabIndex="3" Grid.Column="2" Margin="7,0,0,0" Content="Delete" Style="{StaticResource HyperlinksStyle}" Click="DeleteButtonClick /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> void SendButtonClick(object sender, RoutedEventArgs e) { ... } void DeleteButtonClick(object sender, RoutedEventArgs e) { ... } 的示例 - 填充

Calendar

使用Java-8的示例:

java.sql.Date sqlDate = new java.sql.Date(2017 - 1900, 0, 1); // 2017-01-01
GregorianCalendar gcal = new GregorianCalendar();
gcal.setFirstDayOfWeek(Calendar.MONDAY);
gcal.setMinimalDaysInFirstWeek(4);
gcal.setTime(sqlDate);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-'W'ww");
System.out.println(sdf.format(gcal.getTime())); // 2016-W52

附注:我在这里选择了法国的地区,以确保ISO-8601所需的正确周配置。

使用我的库Time4J 的示例,这只是有趣的,并且如果您还计划对获得的日历周进行一些算术(如java.sql.Date sqlDate = new java.sql.Date(2017 - 1900, 0, 1); // 2017-01-01 LocalDate ld = sqlDate.toLocalDate(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-'W'ww", Locale.FRANCE); System.out.println(dtf.format(ld)); // 2016-W52 或计划到得到一些样式化的本地化输出):

plusWeeks(5)

答案 1 :(得分:0)

TL;博士

org.threeten.extra.YearWeek                       // Handy class found in the ThreeTen-Extra library added to your project.
.from(                                            // Determine the week number and the week-based year number from the passed `LocalDate` object, according to standard ISO 8601 definition of a week.
    myResultSet.getObject( … , LocalDate.class )  // Produces a `LocalDate` object to pass to `YearWeek.from`.  
)                                        
.toString()                                       // Generate a String in standard ISO 8601 format: yyyy-Www
  

2018-W13

详细

当您从H2获取Date类型为java.sql.Date时,请转换为java.time.LocalDate

LocalDate ld = mySqlDate.toLocalDate();

您可以查询ISO 8601标准definition of a week,其中第1周包含一年中的第一个星期四,并且在周一至周日运行。

int weekNumber = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;

提取年份。

int year = ld.getYear();

组装标准ISO 8601字符串。

String output = year + "-W" + String.format( "%02d ", weekNumber );

更简单的方法是使用YearWeek项目中的ThreeTen-Extra类。

String output = YearWeek.from( ld ).toString() ;

JDBC 4.2

从JDBC 4.2及更高版本开始,您可以直接与数据库交换 java.time 对象。无需再次使用java.util或java.sql日期时间类。

LocalDate ld = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) ;  // Capture the current date as seen in the wall-clock used by the people in a certain region (a time zone).
myPreparedStatement.setObject( … , ld ) ;

并检索。

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和&amp; SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

相关问题