This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
Is there a simple function to convert a time that's like this: 10:30pm 17 Aug 2016 NZST to 2016-8-17-2230
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String [] args) throws Exception {
SimpleDateFormat displayFormat = new SimpleDateFormat("yyyy-MM-dd-HHmm");
Date date = new Date();//Current Date
System.out.println(displayFormat.format(date));
}
}
See Java Documentation
http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
Related
This question already has answers here:
Creating java date object from year,month,day
(6 answers)
Why is January month 0 in Java Calendar?
(18 answers)
Closed 10 months ago.
Trying to convert ist into cst but I'm not getting the expected output if, I passes the hardcoded value. Expected output is>
IST:: 18-04-2022 14:11:53
CST:: 18-04-2022 03:41:53
and output I am getting after hardcode value (one month ahead)
IST:: 18-05-2022 14:10:33
CST:: 18-05-2022 03:40:33
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class datetime {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Calendar Local = Calendar.getInstance();
Local.set(2022, 4, 18, 14, 10);
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String ist = formatter.format(Local.getTime());
System.out.println("IST:: " + ist);
Date theist = formatter.parse(ist);
//Convertion of ist into cst
TimeZone cst = TimeZone.getTimeZone("CST");
formatter.setTimeZone(cst);
String cst1 = formatter.format(theist.getTime());
System.out.println("CST:: "+ cst1);
}
}
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:
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:
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.
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.