在go中解析tomcat日志文件的时间戳

时间:2017-11-30 13:21:23

标签: tomcat go timestamp

我试图在go中解析tomcat日志文件的时间戳,但它没有经过。

package main

import (
    "time"
    "fmt"
)

func main() {
    date := "26/Nov/2017:19:23:15 +0000"
    fmt.Println(time.Parse(time.RFC822Z, date))
}

错误信息如下。

0001-01-01 00:00:00 +0000 UTC parsing time "26/Nov/2017:19:23:15 +0000" as "02 Jan 06 15:04 -0700": cannot parse "/Nov/2017:19:23:15 +0000" as " "

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Go有一种相当独特的时间格式化方式。模板需要基于以下日期(可以记住,因为它松散地遵循1,2,3,4,5,6,7):

  

2006年1月2日下午3:04:05 -0700

要创建自定义格式,请创建一个与上述日期格式相匹配的字符串。在您的情况下@Entity @Table(name = "map_record", uniqueConstraints= @UniqueConstraint(columnNames={"map_id", "record_key", "record_value"})) public class MapRecord { @Id @Column(name = "record_id", columnDefinition = "INTEGER NOT NULL") @GeneratedValue(strategy= GenerationType.IDENTITY) public Integer recordId; @Column(name = "map_id") public Integer mapId; @Column(name = "record_key") public String recordKey; @Column(name = "record_value") public String recordValue; @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "record_key", referencedColumnName = "record_key") public List<MapRecord> values = new ArrayList<>(); public void add(String value) { MapRecord mr = new MapRecord(); mr.mapId = mapId; mr.recordKey = recordKey; mr.recordValue = value; values.add(mr); } }

MapHolder mh = new MapHolder(1);
mh.put("key1", "value1");
mh.put("key1", "value2");
em.persist(mh);

https://play.golang.org/p/6nene8tnZL