我的日期为“ 08/08/2019”,我希望它看起来像这样:“ 08,Aug 2019”,我尝试使用<?php
//Assume this line is correct and that you have a database.php file containing your log in credientials
include 'database.php';
//If Statement says - run this next piece of code if $_POST['query'] is set to something
if (isset($_POST['query'])) {
// $search_query = $_POST['query']; - Commented OUT
//This line attempts to sanatise the input from the posted data
$search_query = mysqli_real_escape_string($_POST["query"]);
//This line constructs the whole SQL statement ( BAd methodology here, but thats a different topic)
$query = "SELECT * FROM transporter WHERE email LIKE '%" . $search_query . "%' LIMIT 12";
//You've commented out the next line and its of no use
// $query = "SELECT * FROM transporter WHERE address LIKE %'
//This line has a syntax error - but is also of no use - Should delete but should read $search_query = ' LIMIT 12';
//$search_query ' LIMIT 12";
/// This line queries the database
$result = mysqli_query($link, $query);
//This line declares $data will be an array
$data = array();
//If the DB returns some rows
if (mysqli_num_rows($result) > 0) {
// While there are results
while ($row = mysqli_fetch_assoc($result)) {
//add to the $data array
$data[] = $row["email"];
}
//Output $data in JSON format to be interpreted as a response from your ajax call
echo json_encode($data);
}
}
?>
,但想知道是否有更简便的方法?我知道这是一个很小的问题,但是我试图在互联网上找到答案,但找不到。
答案 0 :(得分:3)
首先,您需要将字符串转换为Date对象,然后使用新的java.time
将其转换为您的格式更新
val firstDate = "08/08/2019"
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val date = formatter.parse(firstDate)
val desiredFormat = DateTimeFormatter.ofPattern("dd, MMM yyyy").format(date)
println(desiredFormat) //08, Aug 2019
旧答案
val firstDate = "08/08/2019"
val formatter = SimpleDateFormat("dd/MM/yyyy")
val date = formatter.parse(firstDate)
val desiredFormat = SimpleDateFormat("dd, MMM yyyy").format(date)
println(desiredFormat) //08, Aug 2019
答案 1 :(得分:1)
Locale englishIsrael = Locale.forLanguageTag("en-IL");
DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(englishIsrael);
DateTimeFormatter mediumDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(englishIsrael);
String dateStringWeHave = "08/08/2019";
LocalDate date = LocalDate.parse(dateStringWeHave, shortDateFormatter);
String dateStringWeWant = date.format(mediumDateFormatter);
System.out.println(dateStringWeWant);
很抱歉,Java语法值得翻译。输出为:
2019年8月8日
与您要求的08, Aug 2019
并不完全相同。但是,Java通常对全球人们期望的格式有一个很好的主意,因此我的第一个建议是您考虑使用该格式(坦白说08
,逗号对我来说也有些奇怪,但是我知道什么? ?)
代码段演示的另一个功能是使用Java.time(现代Java日期和时间API)中的LocalDate
和DateTimeFormatter
。我热烈建议在诸如Date
尤其是SimpleDateFormat
这样过长的日期时间类上使用java.time。他们的设计很差。替换它们是有原因的。
如果用户说他们绝对想要08, Aug 2019
,则需要通过格式模式字符串来指定:
DateTimeFormatter handBuiltFormatter = DateTimeFormatter.ofPattern("dd, MMM uuuu", englishIsrael);
String dateStringWeWant = date.format(handBuiltFormatter);
现在,我们确实获得了您要求的输出:
08,2019年8月
链接: Oracle tutorial: Date Time解释了如何使用Java.time(现代Java日期和时间API)。
答案 2 :(得分:0)
您可以使用Java的SimpleDataFormat类:
import java.text.SimpleDateFormat
代码中的某处:
val myDateStr = "08/08/2019"
val parsedDateObj = SimpleDateFromat("dd/MM/yyyy").parse(myDateStr)
val formattedDateStr = SimpleDateFormat("dd, MMM yyyy").format(parsedDateObj) // "08, Aug 2019"