如何快速设置当前时间?

我试图将纯java代码转换成swift.But,我有一个小的问题,设置当前时间。我可以得到当前时间以毫秒为单位。但我不能设置当前time.Any帮助吗?

这是我的代码

import UIKit class ViewController: UIViewController { override func viewDidLoad() { let now = NSDate() var auctionDate = [String:String]() let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: "en_US") var sdfDDMMYYYYEEE : NSDateFormatter! sdfDDMMYYYYEEE.dateFormat = "dd/MM/yyyy (EEE)" var sdfDDMMYYYY : NSDateFormatter! sdfDDMMYYYY.dateFormat = "dd/MM/yyyy" var sdfEEE : NSDateFormatter! sdfEEE.dateFormat = "EEE" // This is how i get the current time println((now.timeIntervalSince1970)*1000 + 86400000.00) for var i = 0; i < 5; i++ { if sdfEEE.stringFromDate(now) == "Sun" { // This is where i have to set my time with that ((now.timeIntervalSince1970)*1000 + 86400000.00) i--; continue; } auctionDate[(sdfDDMMYYYY.stringFromDate(now) as String)] = (sdfDDMMYYYYEEE.stringFromDate(now) as String) // Here too I have to set my time } println(dateFormatter.stringFromDate(now)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

这是我想要转换成swift代码的java代码

 import java.text.*; import java.util.*; public class DateTest{ Date now = new Date(); Map<String, String> myDate = new LinkedHashMap<String, String>(); myDate.put("", "All"); SimpleDateFormat sdfDDMMYYYYEEE = new SimpleDateFormat("dd/MM/yyyy (EEE)"); SimpleDateFormat sdfDDMMYYYY = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat sdfEEE = new SimpleDateFormat("EEE"); for (int i = 0; i < 5; i++) { if (sdfEEE.format(now).equals("Sun")) { now.setTime(now.getTime() + 86400000); i--; continue; } myDate.put(sdfDDMMYYYY.format(now), sdfDDMMYYYYEEE.format(now)); now.setTime(now.getTime() + 86400000); } } 

任何帮助快速设置当前时间。我几乎接近我的答案。

在你的代码中,你想添加几秒钟到时间1970年。你很幸运,有一个初始化的: NSDate(timeIntervalSince1970: NSTimeInterval)

 let now = NSDate(timeIntervalSince1970: 86400000) 

这将创build一个新的NSDate并在86400000几秒钟内增加86400000秒。