嗨,我想将UTC时间转换为本地时间,而我正在这样做
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";
由于od是Z,所以我得到了解析异常,但是当我这样做
from random import randint
import sys
def guessinggame():
STOP = '='
a = '>'
b = '<'
guess_count = 0
lowest_number = 1
gamecount = 0
highest_number = 100
while True:
guess = (lowest_number+highest_number)//2
print("My guess is :", guess)
user_guess = input("Is your number greater than,less than, or equal to: ")
guess_count += 1
if user_guess == STOP:
break
if user_guess == a:
lowest_number = guess + 1
elif user_guess == b:
highest_number = guess - 1
print("Congrats on BEATING THE GAME! I did it in ", guess_count, "guesses")
PLAY_AGAIN = input("Would you like to play again? y or n: ")
yes = 'y'
gamecount = 0
no = 'n'
if PLAY_AGAIN == yes:
guessinggame()
gamecount = gamecount + 1
else:
gamecount += 1
print("thank you for playing!")
print("You played", gamecount , "games")
sys.exit(0)
return guess_count, gamecount
print('Hello! What is your name?')
myname = input()
print('Well', myname, ', I want you to think of number in your head and I will guess it.')
print("---------------------------------------------------------------------------------")
print("RULES: if the number is correct simply input '='")
print("---------------------------------------------------------------------------------")
print(" if YOUR number is GREATER then the output, input '>'")
print("---------------------------------------------------------------------------------")
print(" if YOUR number is LESS then the output, input '<'")
print("---------------------------------------------------------------------------------")
print(" ALRIGHT LETS PLAY")
print("---------------------------------------------------------------------------------")
guessinggame()
guess_count = guessinggame()
print(" it took me this many number of guesses: ", guess_count)
## each game the user plays is added one to it
## when the user wants to the game to stop they finish it and
## prints number of games they played as well as the average of guess it took
## it would need to take the number of games and add all the guesses together and divide it.
通过,这是否有效?难道不认为sZ是单个常量而不是Z是时区标记吗?
答案 0 :(得分:0)
您正确的说,Z
表示祖鲁时间,这是UTC的另一个名称。您可能还认为它是UTC的零偏移量。因此,您将需要解析Z
作为偏移量,以确保正确解释您的时间。
但是,请勿使用SimpleDateFormat
。众所周知,这很麻烦,而且已经过时了。也不要使用Date
,它也已经过时,并且也存在设计问题。
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
输出:
2018-11-08T21:34:46Z
出于完整性考虑:
想要将…转换为当地时间
以美国/多伦多为例:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
输出:
2018-11-08T16:34:46-05:00 [美国/多伦多]
在评论中您询问了Z
:
如果我根本不包括它,那会有什么区别?
两件事:
Z
解析为偏移量,否则就无法从解析后的值中提取明确的时间点。 链接: Oracle tutorial: Date Time解释了如何使用java.time
。