如何将日期转换为格式“2015-10-26 20:07:45 + 00”
如果我使用UTC-“yyyy-MM-dd'T'HH:mm:ss'Z'”,我会得到“2015-10-26T20:07:45Z”
我应该简单地用空格替换T,用+00替换Z,或者我可以用任何其他格式直接获得我想要的格式吗?
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(now);
答案 0 :(得分:1)
根据<?php
session_start();
unset($_SESSION['isCompleted']);
$isValid = true;
$fullName = "";
$age = "";
$gender = "";
$errorOne = false;
$errorTwo = false;
$errorThree = false;
// checks to see if user has entered a fullname, else throws an error
if (isset($_POST) && !empty($_POST)){
if (isset($_POST['fullName']) && !empty($_POST['fullName'])) {
$fullName = $_POST['fullName'];
}else if ($_SERVER['REQUEST_METHOD'] == "POST"){
$isValid = false;
$error = "Please fill in your Full Name.";
}
// checks to see if user has entered a age, else throws an error
if (isset($_POST['age']) && !empty($_POST['age'])) {
$age = $_POST['age'];
}else if ($_SERVER['REQUEST_METHOD'] == "POST"){
$isValid = false;
$errorTwo = "Please fill in your Age.";
}
// checks to see if user has entered a gender, else throws an error
if (isset($_POST['yourGender']) && !empty($_POST['yourGender'])) {
$country = $_POST['yourGender'];
}else if ($_SERVER['REQUEST_METHOD'] == "POST"){
$isValid = false;
$errorThree = "Please pick your gender";
}
if ($isValid){ //this syntax checks to see that the value == true. If it were a string, it would just need to exist.
header('Location: qp2.php');
}
?>
<html>
<head>
<title> Questions Page 1</title>
</head>
<body>
<form method="post" action="qp1.php">
<!--Name -->
Full Name: <input type="text" name="fullName"/><br/>
<?php if($error){echo $errorOne; }?>
<!-- Email -->
Age: <input type="text" name="age"/><br/>
<?php if($error){echo $errorTwo; }?>
<br/>
<!-- How? Dropdown -->
How did you hear about us?
<select name="yourGender">
<option value=""></option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<?php if($error){echo $errorThree; }?>
<br/>
<!-- Submit button -->
<input type="submit" value="Next"/>
</form>
</body>
</html>
的文档,没有模式符号将ISO-UTC-offset显示为“+00”。最接近的模式符号是“X”,仅当时区偏移偏离UTC时才显示小时偏移。所以我主要看两个选项:
SimpleDateFormat
此处设置文字偏移量仍然可以(例外),因为您在格式对象上明确设置了时区UTC。但它仍然是一个黑客。
否则你可以使用(如果在Java 8 - 使用新的模式符号“x”):
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'+00'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
System.out.println(nowAsISO);
替代方案 - 以及更好的恕我直言 - Java-8中的方式:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssx");
ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC);
String nowAsISO = dtf.format(zdt);
System.out.println(nowAsISO); // 2015-10-27 18:13:15+00
答案 1 :(得分:0)
DateFormat df = new SimpleDateFormat(&#34; yyyy-MM-dd&#39; T&#39; HH:mm:ssz&#34;);