本文整理匯總了Java中java.text.DateFormat.setTimeZone方法的典型用法代碼示例。如果您正苦於以下問題:Java DateFormat.setTimeZone方法的具體用法?Java DateFormat.setTimeZone怎麽用?Java DateFormat.setTimeZone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.DateFormat
的用法示例。
在下文中一共展示了DateFormat.setTimeZone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: convert
import java.text.DateFormat; //導入方法依賴的package包/類
@Override
public String convert(Player o) {
InetAddress rawIp = o.getAddress().getAddress();
String ip = rawIp.toString().replaceAll("/", "");
JSONObject jsonParsed = null;
try {
jsonParsed = JsonReader.readJsonFromUrl("http://freegeoip.net/json/" + ip);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
String jsonValue = (String) jsonParsed.get("time_zone");
Date date = new Date();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setTimeZone(TimeZone.getTimeZone(jsonValue));
return df.format(date);
}
示例2: test_for_objectKey
import java.text.DateFormat; //導入方法依賴的package包/類
public void test_for_objectKey() throws Exception {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", JSON.defaultLocale);
simpleDateFormat.setTimeZone(JSON.defaultTimeZone);
Map<Integer, User> map = new HashMap<Integer, User>();
User user = new User();
user.setId(1);
user.setIsBoy(true);
user.setName("leno.lix");
user.setBirthDay(simpleDateFormat.parse("2012-03-07 22:38:21"));
user.setGmtCreate(new java.sql.Date(simpleDateFormat.parse("2012-02-03 22:38:21").getTime()));
map.put(1, user);
String mapJson = JSON.toJSONString(map, SerializerFeature.WriteClassName, SerializerFeature.WriteMapNullValue);
System.out.println(mapJson);
Object object = JSON.parse(mapJson);
}
示例3: log
import java.text.DateFormat; //導入方法依賴的package包/類
private void log(LogMessage logMessage, LogLevel level, String loggerName) {
String logMessageString;
if (config.getLogInPlainString()) {
StringBuilder sb = new StringBuilder();
sb.append("[CID: ").append(logMessage.getCorrelationId());
if (StringUtils.isNotBlank(logMessage.getCorrelationIdLocal())) {
sb.append(" CID.Local: ").append(logMessage.getCorrelationIdLocal());
}
sb.append("] ").append(logMessage.getMessage());
String varsInString = LogHelper.variablesToString(logMessage.getVariables(), config.getLogVariableType());
if (varsInString.length() > 0) {
sb.append(" [").append(varsInString).append("]");
}
if (logMessage.getThrowable() != null) {
Throwable t = logMessage.getThrowable();
sb.append("\nException: ").append(ExceptionUtils.getMessage(t)).append(" ")
.append(ExceptionUtils.getStackTrace(logMessage.getThrowable()));
sb.append("");
}
logMessageString = sb.toString();
} else {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz);
logMessage.setTimestamp(df.format(new Date()));
logMessage.setLevel(level.toString());
logMessage.setLogger(loggerName);
logMessage.setThread(Thread.currentThread().toString());
Gson gson = new GsonBuilder().disableHtmlEscaping().disableHtmlEscaping().create();
logMessageString = gson.toJson(logMessage);
}
Logger logger = LogManager.getLogger(loggerName);
switch (level) {
case INFO:
logger.info(logMessageString);
break;
case ERROR:
logger.error(logMessageString);
break;
case FATAL:
logger.fatal(logMessageString);
break;
case DEBUG:
logger.debug(logMessageString);
break;
}
}
示例4: formatWithTimeZone
import java.text.DateFormat; //導入方法依賴的package包/類
protected String formatWithTimeZone(
String template,
Object[] arguments,
Locale locale,
TimeZone timezone)
{
MessageFormat mf = new MessageFormat(" ");
mf.setLocale(locale);
mf.applyPattern(template);
if (!timezone.equals(TimeZone.getDefault()))
{
Format[] formats = mf.getFormats();
for (int i = 0; i < formats.length; i++)
{
if (formats[i] instanceof DateFormat)
{
DateFormat temp = (DateFormat) formats[i];
temp.setTimeZone(timezone);
mf.setFormat(i,temp);
}
}
}
return mf.format(arguments);
}
示例5: determineBackupFile
import java.text.DateFormat; //導入方法依賴的package包/類
private File determineBackupFile() {
Constants.Files.EXTERNAL_WALLET_BACKUP_DIR.mkdirs();
checkState(Constants.Files.EXTERNAL_WALLET_BACKUP_DIR.isDirectory(), "%s is not a directory",
Constants.Files.EXTERNAL_WALLET_BACKUP_DIR);
final DateFormat dateFormat = Iso8601Format.newDateFormat();
dateFormat.setTimeZone(TimeZone.getDefault());
for (int i = 0; true; i++) {
final StringBuilder filename = new StringBuilder(Constants.Files.EXTERNAL_WALLET_BACKUP);
filename.append('-');
filename.append(dateFormat.format(new Date()));
if (i > 0)
filename.append(" (").append(i).append(')');
final File file = new File(Constants.Files.EXTERNAL_WALLET_BACKUP_DIR, filename.toString());
if (!file.exists())
return file;
}
}
示例6: getDate
import java.text.DateFormat; //導入方法依賴的package包/類
private String getDate(long timeStamp, String dateFormat)
{
DateFormat objFormatter = new SimpleDateFormat(dateFormat);
String timezone= Calendar.getInstance().getTimeZone().getDefault().getDisplayName();
objFormatter.setTimeZone(TimeZone.getTimeZone(timezone));
Calendar objCalendar =
Calendar.getInstance(TimeZone.getTimeZone(timezone));
objCalendar.setTimeInMillis(timeStamp*1000);
String formatedDate= objFormatter.format(objCalendar.getTime());
objCalendar.clear();
return formatedDate;
}
示例7: fetchFromPool
import java.text.DateFormat; //導入方法依賴的package包/類
private DateFormat fetchFromPool() {
final DateFormat format = pool.fetchFromPool();
final TimeZone tz = timeZone != null ? timeZone : TimeZone.getDefault();
if (!tz.equals(format.getTimeZone())) {
format.setTimeZone(tz);
}
return format;
}
示例8: getFormat
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 獲取指定時區的格式
*
* @return
*/
public DateFormat getFormat(String format, int timeZone) {
DateFormat sdf = new SimpleDateFormat(format);
TimeZone zone = TimeZone.getTimeZone(timeZone < 0 ? "GMT" + timeZone : "GMT+" + timeZone);
sdf.setTimeZone(zone);
return sdf;
}
示例9: formatDate
import java.text.DateFormat; //導入方法依賴的package包/類
private double formatDate(String dateString) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date startDate;
try {
startDate = formatter.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(this.getClass().getName() +
": could not parse time from argument \"" + dateString + "\"");
}
return new Time(startDate).getMJD();
}
示例10: getISO8601FormatDate
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 獲取ISO8601格式化時間
*
* @return 格式化時間
*/
public static String getISO8601FormatDate() {
TimeZone timeZone = TimeZone.getTimeZone("UTC");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); //"Z" to indicate UTC
dateFormat.setTimeZone(timeZone);
return dateFormat.format(new Date());
}
示例11: retainServedDateFormat
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* For Last-Modified and Date headers, we should echo the date back in the exact format we were
* served.
*/
@Test public void retainServedDateFormat() throws Exception {
// Serve a response with a non-standard date format that OkHttp supports.
Date lastModifiedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-1));
Date servedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-2));
DateFormat dateFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("EDT"));
String lastModifiedString = dateFormat.format(lastModifiedDate);
String servedString = dateFormat.format(servedDate);
// This response should be conditionally cached.
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + lastModifiedString)
.addHeader("Expires: " + servedString)
.setBody("A"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
// The first request has no conditions.
RecordedRequest request1 = server.takeRequest();
assertNull(request1.getHeader("If-Modified-Since"));
// The 2nd request uses the server's date format.
RecordedRequest request2 = server.takeRequest();
assertEquals(lastModifiedString, request2.getHeader("If-Modified-Since"));
}
示例12: _logCommError
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 通訊錯誤日誌
*/
private static void _logCommError(Exception e, HttpURLConnection conn, String url,
String appKey, String method, Map<String, String> params) {
DateFormat df = new SimpleDateFormat(AlipayConstants.DATE_TIME_FORMAT);
df.setTimeZone(TimeZone.getTimeZone(AlipayConstants.DATE_TIMEZONE));
String sdkName = AlipayConstants.SDK_VERSION;
String urlStr = null;
String rspCode = "";
if (conn != null) {
try {
urlStr = conn.getURL().toString();
rspCode = "HTTP_ERROR_" + conn.getResponseCode();
} catch (IOException ioe) {
}
} else {
urlStr = url;
rspCode = "";
}
StringBuilder sb = new StringBuilder();
sb.append(df.format(new Date()));// 時間
sb.append("^_^");
sb.append(method);// API
sb.append("^_^");
sb.append(appKey);// APP
sb.append("^_^");
sb.append(getIp());// IP地址
sb.append("^_^");
sb.append(osName);// 操作係統
sb.append("^_^");
sb.append(sdkName);// SDK名字,這是例子,請換成其他名字
sb.append("^_^");
sb.append(urlStr);// 請求URL
sb.append("^_^");
sb.append(rspCode);
sb.append("^_^");
sb.append((e.getMessage() + "").replaceAll("\r\n", " "));
clog.error(sb.toString());
}
示例13: retainServedDateFormat
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* For Last-Modified and Date headers, we should echo the date back in the exact format we were
* served.
*/
@Test public void retainServedDateFormat() throws Exception {
// Serve a response with a non-standard date format that OkHttp supports.
Date lastModifiedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-1));
Date servedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-2));
DateFormat dateFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("EDT"));
String lastModifiedString = dateFormat.format(lastModifiedDate);
String servedString = dateFormat.format(servedDate);
// This response should be conditionally cached.
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + lastModifiedString)
.addHeader("Expires: " + servedString)
.setBody("A"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
// The first request has no conditions.
RecordedRequest request1 = server.takeRequest();
assertNull(request1.getHeader("If-Modified-Since"));
// The 2nd request uses the server's date format.
RecordedRequest request2 = server.takeRequest();
assertEquals(lastModifiedString, request2.getHeader("If-Modified-Since"));
}
示例14: logErrorScene
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 發生特別錯誤時記錄完整錯誤現場
*/
public static void logErrorScene(Map<String, Object> rt, AlipayResponse tRsp,
String appSecret) {
if (!needEnableLogger) {
return;
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone(AlipayConstants.DATE_TIMEZONE));
StringBuilder sb = new StringBuilder();
sb.append("ErrorScene");
sb.append("^_^");
sb.append(tRsp.getErrorCode());
sb.append("^_^");
sb.append(tRsp.getSubCode());
sb.append("^_^");
sb.append(ip);
sb.append("^_^");
sb.append(osName);
sb.append("^_^");
sb.append(df.format(new Date()));
sb.append("^_^");
sb.append("ProtocalMustParams:");
appendLog((AlipayHashMap) rt.get("protocalMustParams"), sb);
sb.append("^_^");
sb.append("ProtocalOptParams:");
appendLog((AlipayHashMap) rt.get("protocalOptParams"), sb);
sb.append("^_^");
sb.append("ApplicationParams:");
appendLog((AlipayHashMap) rt.get("textParams"), sb);
sb.append("^_^");
sb.append("Body:");
sb.append((String) rt.get("rsp"));
blog.error(sb.toString());
}
示例15: convertDateToString
import java.text.DateFormat; //導入方法依賴的package包/類
public static String convertDateToString(Date date, String pattern) {
DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
pattern);
if (Validator.isNull(date) || Validator.isNull(pattern)) {
return StringPool.BLANK;
}
dateFormat.setTimeZone(TimeZoneUtil.getTimeZone("Asia/Ho_Chi_Minh"));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return dateFormat.format(calendar.getTime());
}