如何在Java 5中使用这行代码,因为此代码使用threetenbp与Java 6和7一起运行,并直接在Java 8上运行:
// start date (set the time to start of day)
ZonedDateTime from = LocalDate.parse("2017-07-05").atStartOfDay(ZoneOffset.UTC);
// end date (set the time to 11 PM)
ZonedDateTime to = LocalDate.parse("2017-07-08").atTime(23, 0).atZone(ZoneOffset.UTC);
// get start and end timestamps
long start = from.toInstant().truncatedTo(ChronoUnit.MINUTES).toEpochMilli() / 1000;
long end = to.toInstant().truncatedTo(ChronoUnit.MINUTES).toEpochMilli() / 1000;
答案 0 :(得分:2)
ThreeTen Backport似乎仅适用于Java 6和7.我已经使用JDK 5进行了测试,并抛出import UIKit
class VC: UIViewController {
@IBOutlet weak var usernameTxt: UITextField!
@IBOutlet weak var emailTxt: UITextField!
@IBOutlet weak var passTxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
passTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
}
}
。
在Java 5中,一种替代方法是旧的UnsupportedClassVersionError
和SimpleDateFormat
类:
Calendar
另一种方法是使用Joda-Time,其API与// set the formatter to UTC
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(utc);
// parse and set the time to midnight
Calendar cFrom = Calendar.getInstance(utc);
cFrom.setTime(sdf.parse("2017-07-05"));
cFrom.set(Calendar.HOUR_OF_DAY, 0);
cFrom.set(Calendar.MINUTE, 0);
cFrom.set(Calendar.SECOND, 0);
cFrom.set(Calendar.MILLISECOND, 0);
// parse and set the time to 23:00
Calendar cTo = Calendar.getInstance(utc);
cTo.setTime(sdf.parse("2017-07-08"));
cTo.set(Calendar.HOUR_OF_DAY, 23);
cTo.set(Calendar.MINUTE, 0);
cTo.set(Calendar.SECOND, 0);
cTo.set(Calendar.MILLISECOND, 0);
// get the epoch second (get millis and divide by 1000)
long start = cFrom.getTimeInMillis() / 1000;
long end = cTo.getTimeInMillis() / 1000;
非常相似:
java.time
这将为import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
DateTime from = LocalDate.parse("2017-07-05").toDateTimeAtStartOfDay(DateTimeZone.UTC);
DateTime to = LocalDate.parse("2017-07-08").toDateTime(new LocalTime(23, 0), DateTimeZone.UTC);
long start = from.getMillis() / 1000;
long end = to.getMillis() / 1000;
和start
生成相同的值。
请注意:Joda-Time处于维护模式,正在被新的API取代,所以我不建议用它来开始一个新项目(除非你不能使用新的API&# 39; s,当然)。
即使在joda's website它也说:"请注意,Joda-Time被认为是一个很大程度上“完成”的项目。没有计划重大改进。如果使用Java SE 8,请迁移到java.time(JSR-310)。" 。