This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I need to convert a String which is a epoch (Unix time) format to a Date class an after a String formatted (dd/MM/yyyy).
Thank you for your help !
Unix time is the number of seconds since 1 January 1970, so this should work
Date date = new Date(unixTime * 1000);
String str = new SimpleDateFormat("dd/MM/yyyy").format(date);
BTW SimpleDateFormat accepts millis as argument too, so it is possible to get the same result as
String str = new SimpleDateFormat("dd/MM/yyyy").format(unixTime * 1000);
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);
Related
This question already has answers here:
Comparing time is incorrect when picking 12:00
(3 answers)
Closed 4 years ago.
I tried to convert a String to Date by the following way in Java:
public static Date dateConv1(String s){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
String dateInString = s;
Date date = new Date();
It converts the noon 12:00-12:59 as midnight i.e 00:00-00:59. Could soeone let me know how I should solve this issue?
Change your formatter:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HH - means hours
Reach to: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and see all the examples you need.
The String is not correctly formated. hh means 1-12 hours. There are 4 way to format hours (hh, HH, kk, KK).
Here you can read about the formatting:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
" hh " is for 12 hours format change it to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I have TimeStamp time as String 1374160160 which is equivalent 07/18/2013.
If there is a way how I can convert this TimeStamp to date with format July 18, 2013 ?
This is my code snippet:
try{
String timeStamp = "1374160160";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
} catch(Exception ex) {
return "";
}
It always returns 01/17/1970 why?
The second problem is that new Date(String) is deprecated.
To sum up:
How to create normal date (July 18, 2013) from time-stamp and avoid the deprecated methods ?
Your timeStamp instance holds seconds but Java Date constructor expects milliseconds. Via documentation:
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
So you should use this:
String timeStamp = "1374160160";
long timeMillis = Long.valueOf(timeStamp) * 1000;
Date netDate = new Date(timeMillis);
instead of:
String timeStamp = "1374160160";
Date netDate = (new Date(timeStamp));
This question already has answers here:
Converting 24hour time to 12hour time?
(6 answers)
Closed 6 years ago.
I just want to convert a date from the database to 12 hours format which is originally in 24 hours format which is something like this 2015-06-11T28:28:57.000Z. In here this time format (28:25:57) seems to be the next day (ie. 2015-06-12T04:28:57).
I have tried with the following:
String date = "2015-06-11T28:28:57.000Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a");
Date date = formatter.parse(date);
but I got an error like unparsable date.
Your date formatter string was incorrect, Try this:
String dateStr = "2015-06-11T28:28:57.000Z";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = sdfInput.parse(dateStr);
System.out.println("Date:"+date ); //Fri Jun 12 04:28:57 IST 2015
// to get 12 hour date formate date-string from provided date
SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS ");
String dateStr2 = sdfOutput.format(date);
System.out.println("dateStr2:" + dateStr2); //2015-06-12 04:28:57.000
This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a String value which I need to convert into time and save it in the MySQL.[The column's datatype is time in the database table]
The examples of String values that I might encounter to convert into time object are:
String time = "5:32 PM";
String time = "12:00 AM";
String time = "11:43 PM";
I browsed for few examples which pulled the time from the Date object but couldn't find an apt example, such as in this case to convert it from a plain string. The main reason I need to convert it to time is to save it in the mysql.
You can convert it to java.sql.Date as :
String str = "5:32 PM";
DateFormat formatter = new SimpleDateFormat("hh:mm a");
java.util.Date date = (java.util.Date)formatter.parse(str);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
If you need java.sql.Time , then :
java.sql.Time time = new java.sql.Time(date.getTime());
And then use PreparedStatement#setTime().