我的方法是:
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
String date = null;
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
try{
Date date3 = df.parse(currentDate);
df.format(date3);
System.out.println("date3 " +date3);
Date previousDate = DateUtils.addDays(date3, variant);
date = previousDate.toString();
return date;
}catch (Exception e){
}
return date;
}
注意:currentTime变量的值始终为" 18/12 / 2017" 我希望以dd / mm / yyyy格式显示日期结果。但它始终给出" Wed Jan 18 00:12:00 IST 2017"像这样。
运行时间结果:
currentDate 18/12/2017
date3 Wed Jan 18 00:12:00 IST 2017
答案 0 :(得分:1)
您应该返回格式化日期,而不是日期的toString()。试试这个:
Date previousDate = DateUtils.addDays(date3, variant);
return df.format(previousDate);
答案 1 :(得分:0)
df.format只返回应用了格式的Date的String表示,因此代码中的行无效。
尝试更改代码以改为使用格式化输出:
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date3 = df.parse(currentDate);
System.out.println("date3 " + df.format(date3));
Date previousDate = DateUtils.addDays(date3, variant);
return previousDate.toString();
}
此外 - 它很难捕获异常,所以你应该删除它。
答案 2 :(得分:0)
我们应该总是试图找出更方便的方式,以便我们可以进一步使用它。在这种情况下,我们可以使用以下方法:
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim jsonName = New IO.StreamReader(DirectCast(Await DirectCast(Net.WebRequest.Create("https://www.googleapis.com/youtube/v3/channels?part=snippet&id=" & ChannelID & "&fields=items(id%2Csnippet(title))&key=AIzaSyA1n4M-fo2Y5NHUj0RsvXEAis3H6_lIjRg"), Net.HttpWebRequest).GetResponseAsync, Net.HttpWebResponse).GetResponseStream)
Dim rqn As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JObject.Parse(jsonName.ReadToEnd)
ChannelName = $"{rqn.SelectToken("items")(0)("snippet")("title")}"
Label3.Text = ChannelName
Dim json = New IO.StreamReader(DirectCast(Await DirectCast(Net.WebRequest.Create("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" & ChannelID & "&fields=items(statistics(subscriberCount))&key=AIzaSyA1n4M-fo2Y5NHUj0RsvXEAis3H6_lIjRg"), Net.HttpWebRequest).GetResponseAsync, Net.HttpWebResponse).GetResponseStream)
Dim rq As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JObject.Parse(json.ReadToEnd)
Subs = $"{Convert.ToDecimal(rq.SelectToken("items")(0)("statistics")("subscriberCount")).ToString("#,###,###")}"
Label1.Text = Subs
Catch ex As Exception
Timer1.Stop()
MessageBox.Show("Erro ao tentar Conseguir os Dados do Canal!")
End Try
End Sub
您可以轻松调用此方法,如下所示:
public String dateString(String input) {
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String formattedDate = "";
try {
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formattedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
返回如下输出:
Date date3 = df.parse(currentDate);
df.format(date3);
String input = date3.toString();
String requiredDate = dateString(input);
System.out.println("requiredDate: "+ requiredDate);