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);
}
}
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:
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:
Convert java.util.Date to String
(17 answers)
Closed 6 years ago.
newDate =(newDaysAdded+"/"+month+"/"+year);
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
try {
Date newDateOne = date.parse(newDate);
When I try:
view.writeToScreen(newDateOne);
I get the error:
Incompatible types: Date cannot be converted to String.
Here is the writeToScreen method:
void writeToScreen(String s);
However from there I can't turn into a string, I have tried:
String test = Date.toString(newDateOne);
Can anyone help?
Based on your existing code you could use:
writeToScreen (date.format(newDateOne));
Change
view.writeToScreen(newDateOne);
to
view.writeToScreen(newDateOne.toString());
Try to look at all the methods... Either in the API doc or even in the intellisense in a code editor
public static void main(String[] args) {
String newDaysAdded = "05";
String month = "04";
String year = "2014";
String newDateString = (newDaysAdded+"/"+month+"/"+year);
SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
try {
Date newDate = SDF.parse(newDateString); //Takes string and convert it to date
//If i understand the question you dont just want to print it as a date...
System.out.println(newDate); //Sat Apr 05 00:00:00 CAT 2014
//You want to print it as a string inf the format you specified in the SDT
System.out.println(SDF.format(newDate)); //Print as in05/04/2014
} catch (ParseException e) {
e.printStackTrace();
}
}
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-How to calculate accurate time difference while using Joda Time Jar
(2 answers)
Closed 7 years ago.
I have used Jodha Library and tried to get the difference between two dates in seconds. But it is only accurate up to the date. Not to the seconds.
public static int getDateDifference(DateTime dateCreatedPa)
{
LocalDate dateCreated = new LocalDate (dateCreatedPa);
LocalDate now = new LocalDate();
Seconds secondsBetween = Seconds.secondsBetween(dateCreated, now);
return secondsBetween.getSeconds();
}
///code calling the above method
DateTime dateCreated=new DateTime(drivingLicense.getDateCreated());
int dateDiff=Common.getDateDifference(dateCreated);
request.setAttribute("dateDiff", dateDiff);
System.out.println("Timestamp: "+dateDiff);
This shows the date difference. But if I give different times on the same date for comparison, it returns 0.
LocalDate is just that, date only, no time information. Use LocalDateTime instead
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Seconds;
public class Test {
public static void main(String[] args) {
DateTime today = DateTime.now();
today = today.minusDays(5);
int dateDiff = getDateDifference(today);
System.out.println("Timestamp: " + dateDiff);
}
public static int getDateDifference(DateTime dateCreatedPa) {
LocalDateTime dateCreated = new LocalDateTime(dateCreatedPa);
LocalDateTime now = new LocalDateTime();
System.out.println(dateCreated);
System.out.println(now);
Seconds secondsBetween = Seconds.secondsBetween(dateCreated, now);
return secondsBetween.getSeconds();
}
}
Outputs...
2015-02-27T15:20:56.524
2015-03-04T15:20:56.628
Timestamp: 432000