英文:
gregorian calendars package on golang
问题
我正在一个项目上工作,我想在Go语言中使用公历来创建一个日期变量。我已经搜索过了,但没有找到答案。以下是我想在Go语言中用Java类型代码实现的内容:
import (
"time"
)
func main() {
callEndDateTime := "2023-09-07 12:00:00"
duration := 60
layout := "2006-01-02 15:04:05"
t, err := time.Parse(layout, callEndDateTime)
if err != nil {
fmt.Println("Couldn't parse the given date:", callEndDateTime)
return
}
t = t.Add(-time.Second * time.Duration(duration))
callStartDateTime := t.Format(layout)
fmt.Println(callStartDateTime)
}
希望能帮到你!
英文:
I m wrking on a project, and I want to make a date variable with the gregorian calendar in goLang, I've searched about it, but i didn't found an answer
here is what i want to do with in golang in a java type code
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;
}
thanks for helping me!
答案1
得分: 0
你可以将它作为一个字符串变量,并进行解析。
string strDate = "07 31 2017"; //示例
DateFormat df = new SimpleDateFormat("dd MM yyyy");
Date date = df.parse(strDate);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
英文:
You can make it as a string variable and than parse it.
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);
答案2
得分: 0
时间包始终假定你使用的是公历,文档中有详细说明。
如果你想在Go语言中解析日期时间,这是相当简单的,但你必须记住日期解析器并不使用“标准”的日期格式定义方式。
你可以在官方文档中看到如何使用时间解析器的示例:
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
fmt.Println(t)
这就是在Go语言中将字符串解析为日期的方法。将日期转换为字符串甚至更简单:
t.Format("2006-01-02")
更多信息请参阅文档。
英文:
The time package always assumes you're working with a Gregorian calendar, as shown in the documentation
Now if you want to parse a date time in golang it's rather simple but you have to keep in mind that the date parser isn't using the "standard" way of defining date formats.
You can see an example of how to use the time parser in 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)
That's how you parse a string into a date in go. Converting a date to a string is even simpler :
t.Format("2006-01-02")
See the documentation for more information
1: https://golang.org/pkg/time/ "See here"
2: https://golang.org/pkg/time/#Parse
3: https://golang.org/pkg/time/#Time.Format
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论