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.
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:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Java string to date conversion
(17 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
display Java.util.Date in a specific format
(11 answers)
Java Android Calendar/DateFormat this format XXXXXXXX [closed]
(4 answers)
Closed 3 years ago.
I need convert String to Date with next format:
String date1 ="26032018";
// ddmmyyyy
I need:
032618
//mmddyy
I tried:
Date date2 = new SimpleDateFormat("ddmmyy").parse(date1);
But don't work
Output:
Thu Jan 03 00:26:00 CLST 2008
Any idea?
Works for me
String date1 ="26032018";
Date date2 = null;
try {
date2 = new SimpleDateFormat("ddMMyyyy").parse(date1);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("MMddyy").format(date2));
String date1 ="26032018";
String pattern ="MMddyyyy";
Date formatedDate = new SimpleDateFormat("ddMMyyyy").parse(date1);
String d = new SimpleDateFormat(pattern).format(formatedDate);
This question already has answers here:
Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string
(5 answers)
Closed 5 years ago.
This is the question i got in an online interview test
Output format is like
20th Oct 2052 converts to 2052-10-20.
6th Jun 1933 converts to 1933-06-06.
Can any one please write a method to return an array of strings where each index i contains the value of dates converted to the format YYYY-MM-DD.
This is the code to Convert 1st March 1984 to 1984-03-01 By using Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormattingTest {
public void date(String dateString) throws ParseException {
// This Regular Expression will replace st to blank
String dateString1 = dateString.replaceFirst("[a-zA-Z]{2}", "");
//create Date Format and Parse it based on input
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d MMMM yyyy");
Date rightNow = simpleDateFormat.parse(dateString1);
// Now create Date format for output type. and format the input
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = dateFormat.format(rightNow);
System.out.println(formattedDate);
}
public static void main(String[] args) throws ParseException {
String dateString = "1st March 1984";
DateFormattingTest t = new DateFormattingTest();
t.date(dateString);
}
}
In the above code input is hard coded String dateString = "1st March 1984";
it gives the correct output
But according to the question input should be array of strings in the form of Day Month Year and out put should return an array of strings in the format YYYY-MM-DD. I wrote the dates() method similar to the date() method in the above code. in the solution main method handling IOException it is pre written main method we are supposed to pass our method in main method to return the output but here my method is handling ParseException which is not handled by main method in the given program so i am getting ParseException handling error so is there any other way of writing function to give the output
"d MMMM yyyy" requires month in local language, try to set SimpleDateFormat's Locale to English. Also, theres no need to delete "st", try "d'st' MMMM yyyy"
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.
This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 6 years ago.
In java if I have a String that looks like this: "Nov 30, 2016" (this is FormatStyle.MEDIUM), how do I convert it into a LocalDateTime data type?
I ran this on JDK 8:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class DateFormatDemo {
public static void main(String[] args) {
String dateStr = "Nov 30, 2016";
LocalDateTime ldt = LocalDateTime.parse(dateStr, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("date string: " + dateStr);
System.out.println("local date : " + ldt);
}
}
Here's my output:
date string: Nov 30, 2016
local date : 2016-11-30
Process finished with exit code 0
I got an exception with the original code using LocalDateTime, because the input String didn't include a time.
If you'd like LocalDateTime, try adding hh:mm:ss to the input.