根据SimpleDateFormat class documentation , Java不支持在其日期模式中超过毫秒的时间粒度。
所以,像这样的日期字符串
. 之后的整数符号为(近 10 亿!)毫秒而不是纳秒,导致日期S符号仍然会导致解析所有 9 位数字(而不是最左边的 3 位 .SSS)。SimpleDateFormat实现,没有任何其他代码修改或字符串操作?
最佳答案
tl;博士
LocalDateTime.parse( // With resolution of nanoseconds, represent the idea of a date and time somewhere, unspecified. Does *not* represent a moment, is *not* a point on the timeline. To determine an actual moment, place this date+time into context of a time zone (apply a `ZoneId` to get a `ZonedDateTime`).
"2015-05-09 00:10:23.999750900" // A `String` nearly in standard ISO 8601 format.
.replace( " " , "T" ) // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
) // Returns a `LocalDateTime` object.
Java does not support time granularity above milliseconds in its date patterns
SimpleDateFormat 和相关的 java.util.Date/.Calendar 类现在已被 Java 8 ( java.time ) 中的新 Tutorial 包过时了。java.time.format DateTimeFormatter API 时,S 模式字母表示“秒的分数”而不是“毫秒”,它可以处理纳秒值。InstantInstant 类代表 UTC 中的一个时刻。它的 toString 方法使用标准的 ISO 8601 格式生成一个 String 对象。末尾的 Z 表示 UTC,发音为“Zulu”。instant.toString() // Generate a `String` representing this moment, using standard ISO 8601 format.
2013-08-20T12:34:56.123456789Z
Clock 的实现。在 Java 9 及更高版本中,新的 Clock 实现可以更精细地捕捉当前时刻,具体取决于主机硬件和操作系统的限制,根据我的经验,通常是 microseconds。Instant instant = Instant.now() ; // Capture the current moment. May be in milliseconds or microseconds rather than the maximum resolution of nanoseconds.
LocalDateTime2015-05-09 00:10:23.999750900 示例输入字符串缺少时区或 UTC 偏移量的指示符。这意味着它不代表一个时刻,不是时间线上的一个点。相反,它代表了大约 26-27 小时范围内的潜在时刻,即全局时区的范围。LocalDateTime 对象。首先,将中间的 SPACE 替换为 T 以符合 ISO 8601 格式,在解析/生成字符串时默认使用。所以不需要指定格式模式。LocalDateTime ldt =
LocalDateTime.parse(
"2015-05-09 00:10:23.999750900".replace( " " , "T" ) // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
)
;
java.sql.Timestampjava.sql.Timestamp 类也处理纳秒级分辨率,但以一种笨拙的方式。通常最好在 java.time 类中完成您的工作。从 JDBC 4.2 及更高版本开始,无需再次使用 Timestamp。myPreparedStatement.setObject( … , instant ) ;
Instant instant = myResultSet.getObject( … , Instant.class ) ;
OffsetDateTimeInstant,但 OffsetDateTime 是。因此,如果上述代码与您的 JDBC 驱动程序失败,请使用以下代码。OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;
Instant instant = myResultSet.getObject( … , OffsetDateTime.class ).toInstant() ;
toInstant 和 from 方法在 java.sql.Timestamp 和 java.time 之间来回切换。这些新的转换方法被添加到旧的遗留类中。
java.util.Date 、 Calendar 和 SimpleDateFormat 。java.sql.* 类。Interval 、 YearWeek 、 YearQuarter 和 more 。
关于具有微秒或纳秒精度的 Java 日期解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135025/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s