This question already has answers here:
Java: How to get current date in ISO 8601 SECOND format
(2 answers)
Closed 6 years ago.
I have the TimeStamp and I need to convert it to Data type object that should match this pattern - "2016-11-16T18:42:33.049Z". How can I do that?
Date d = new Date((long)timestamp*1000);
will create a Date instance. Displaying it later is another thing.
I think it's what you want:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
System.out.println(f.format(date));
Test:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
Date d = new Date((long)1481723817*1000);
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
System.out.println(f.format(d));
}
}
>>2016-12-14T14:56:57.056Z
Convert like this.
String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
you can also use the data object in the manner u want.
Related
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Why does sdf.format(date) converts 2018-12-30 to 2019-12-30 in java? [duplicate]
(3 answers)
Closed 2 years ago.
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
date1 = dateFormat.format(new Date(date));
System.out.println(date+" "+date1);
My input is date = 30-Dec-2019 and the expected output is 2019-12-30
The output I'm getting is 2020-12-30
Do not use the outdated date/time API. Do it using modern date/time API as follows:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) throws Exception {
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String strDate = "30-Dec-2019";
LocalDate date = LocalDate.parse(strDate, inputFormat);
System.out.println(outputFormat.format(date));
}
}
Output:
2019-12-30
Check this to learn about the drawbacks of the outdated date/time API and the benefits of modern date/time API.
This question already has answers here:
Java SimpleDateFormat Timezone offset with minute separated by colon
(2 answers)
Closed 3 years ago.
I'm using java's SimpleDateFormat, here is my code:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
String strDate = simpleDateFormat.format(date);
System.out.println(strDate);
Which print out:
2019-11-15T11:59:47.289+0200
But, I want to have a colon inside the offset, which means it need to look like this:
2019-11-15T11:59:47.289+02:00
Is there a way to adding a time zone that printed out like the second example here?
What you are talking about is not a time zone (like UTC), it is an offset (like +01:00).
You can use the modern date time API java.time, which has a built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME, that formats the offset as desired:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class StackoverflowDemo {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.now();
System.out.println(odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
}
The output on my system is this:
2019-11-15T11:30:46.532+01:00
this snippet below gives result like this
2019-11-15T16:03:53+05:30
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(dateFormat2.format(new Date()));
hope this is what you are looking for
Your format should be yyyy-MM-dd'T'hh:mm:ss.SSSXXX
I think your best bet is to use DateTimeFormatter.
You can find documentation here:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 3 years ago.
DateFormat dateFormatOne = new SimpleDateFormat("HH:mm:ss");
dateFormatOne.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateOne = dateFormatOne.parse("10:00:00");
format convert date to string
parse convert string to date
public static void main(String[] args) throws ParseException {
DateFormat dateFormatOne = new SimpleDateFormat("HH:mm:ss");
dateFormatOne.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateOne = dateFormatOne.parse("10:00:00");
System.out.println(dateFormatOne.format(dateOne));
System.out.println(dateFormatOne.format(new Date()));
}
Output
10:00:00
20:20:55
But I recommend Java 8
System.out.print(LocalTime.of(10, 0, 0));
If you want to display the current local time on the machine running the program you can use LocalTime.
import java.time.LocalTime; // import the LocalTime class
public class Main {
public static void main(String[] args) {
LocalTime myTime = LocalTime.now();
System.out.println(myTime);
}
}
This question already has answers here:
Can’t rid of 'T' in LocalDateTime
(3 answers)
Localdate.format, format is not applied
(2 answers)
Closed 4 years ago.
I am new to Java8 and looking to convert the String into Date in order ro save it in Redis. I went through https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html and tried to Patterns for Formatting and Parsing as per my requirement.
public class DateDemo {
public static void main(String[] args) {
DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String date = "10-10-2018 12:10:17";
LocalDateTime parse = LocalDateTime.parse(date, DATE_FORMAT);
System.out.println("PARSE = "+parse);
}
}
Output: PARSE = 2018-10-10T12:10:17
This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I am trying to convert a string with milliseconds (20180510-10:50:58.106) to date in java. However, when I convert it I get the millisecond part but again in the string. I want the same part in date format.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateFormatExample {
public static void main(String[] args) {
String curDate = "20180510-10:50:58.106";
Date SysDateVar = null ;
//SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
//String Todate = format.format(curDate);
//System.out.println("format.parse(curDate)="+Todate);
String abc = null;
try
{
abc = format.format(format.parse(curDate));
System.out.println("SysDateVar ="+abc);
SysDateVar = new Date ((long) (format.parse(curDate)).getTime());
System.out.println("format.parse(curDate)="+ SysDateVar);
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
Output Is :
$javac SimpleDateFormatExample.java
$java -Xmx128M -Xms16M SimpleDateFormatExample
SysDateVar =20180510-10:50:58.106
format.parse(curDate)=Thu May 10 10:50:58 UTC 2018
Variable abc is a string. I want the same output in a Date variable.
When you call System.out.println on a Date object, the toString() method of this Date will be called implictly, and it will return in default format like Thu May 10 10:50:58 UTC 2018.
You need call format.format(date) explictly to get desired output.