2017-07-31 47 views
0

我正在写一个项目,我想用goLang中的公历做一个日期变量,我已经搜索过它,但我没有找到答案 这里是我的想用java编写的golang中的代码golang上的公历日历包

try { 
       final Calendar gc = new GregorianCalendar(); 
       gc.setTime(simpleDateFormat.parse(callEndDateTime)); 
       gc.add(Calendar.SECOND, -1 * duration); 
       callStartDateTime = simpleDateFormat.format(gc.getTime()); 
      } catch (final ParseException parseException) { 
       LOGGER.error("Couldn't parse the given date: " + callEndDateTime, parseException); 
       callStartDateTime = null; 
      } 

感谢帮助我!

+0

顺便说看到documentation,麻烦'Calendar'类现在是传统的,由java.time类,如取代' ZonedDateTime'。 –

回答

0

您可以将其设置为字符串变量并进行解析。

string strDate = "07 31 2017"; //example 
DateFormat df = new SimpleDateFormat("dd MM yyyy"); 
Date date = df.parse(strDate); 
Calendar cal = new GregorianCalendar(); 
cal.setTime(date); 
+0

但我想在golang中的代码不是java –

+0

您可以使用Time.String()方法将time.Time转换为字符串。 t:= time.Now() fmt.Println(t.String()) –

+0

它将显示为simpleDate? –

0

的时间包总是假定你有一个公历工作,as shown in the documentation

现在,如果你想在golang这是相当简单的解析的日期时间,但你要记住,日期解析器没有使用定义日期格式的“标准”方式。

你可以看到如何使用时间解析器的例子the official documentation

const longForm = "Jan 2, 2006 at 3:04pm (MST)" 
t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") 
fmt.Println(t) 

这就是你如何解析字符串到去约会。转换日期为字符串更简单:

t.Format("2006-01-02") 

了解更多信息