将当前年度的总天数转换为一个月中的一天

时间:2015-08-07 17:36:16

标签: java date simpledateformat

我正在将有效负载 1430848842000 的时间戳转换为SimpleDateFormat(yyy-MM-DD HH:mm:ss)。我的输出是:

  

2015-05-125 14:00:42

如您所见,转换的日期部分很奇怪。它没有给我今年五月份的日期,而是给了我创建时间戳前一年的总天数。

我做了一些快速数学并确定125时间戳应该真的意味着5月5日,我怎么能自动转换这个,所以我的输出看起来像:

  

2015-05-5 14:00:42

2 个答案:

答案 0 :(得分:4)

您的SimpleDateFormat应为yyyy-MM-DD HH:mm:ss而不是public String sendFileToServer(String filename, String targetUrl) { String response = "error"; Log.e("Image filename", filename); Log.e("url", targetUrl); HttpURLConnection connection = null; DataOutputStream outputStream = null; // DataInputStream inputStream = null; String pathToOurFile = filename; String urlServer = targetUrl; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss"); int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024; try { FileInputStream fileInputStream = new FileInputStream(new File( pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setChunkedStreamingMode(1024); //connection.set // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); String connstr = null; connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "uploadedfile.log" + "\"" + lineEnd; Log.i("Connstr", connstr); outputStream.writeBytes(connstr); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); Log.e("Image length", bytesAvailable + ""); try { while (bytesRead > 0) { try { outputStream.write(buffer, 0, bufferSize); } catch (OutOfMemoryError e) { e.printStackTrace(); response = "outofmemoryerror"; return response; } bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } } catch (Exception e) { e.printStackTrace(); response = "error"; return response; } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) int serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); Log.i("Server Response Code ", "" + serverResponseCode); Log.i("Server Response Message", serverResponseMessage); if (serverResponseCode == 200) { response = "true"; } String CDate = null; Date serverTime = new Date(connection.getDate()); try { CDate = df.format(serverTime); } catch (Exception e) { e.printStackTrace(); Log.e("Date Exception", e.getMessage() + " Parse Exception"); } Log.i("Server Response Time", CDate + ""); filename = CDate + filename.substring(filename.lastIndexOf("."), filename.length()); Log.i("File Name in Server : ", filename); fileInputStream.close(); outputStream.flush(); outputStream.close(); outputStream = null; } catch (Exception ex) { // Exception handling response = "error"; Log.e("Send file Exception", ex.getMessage() + ""); ex.printStackTrace(); } return response; } 。那就是" DD "是" dd "代替。

答案 1 :(得分:1)

正如其他人所提到的,您使用的格式错误来表示当月的日期。

您应该使用dd代替DD

mm:分钟代表

MM:当月的代表

dd:日代表

DD:代表一年中的某一天(例如:365个中的50个)

hh: 12小时时钟表示

HH: 24小时制表示

实施例

import java.text.SimpleDateFormat;

Date date_now = new Date();

date_format = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
date_string = date_format.format(date_now);
System.out.println(date_string);

输出:

2014-8-07 1:51:12