How format duration hours:minutes:seconds [duplicate] - java

This question already has answers here:
How to format a duration in java? (e.g format H:MM:SS)
(22 answers)
Closed 3 years ago.
I need format java.time.Duration like
T05:00:00 (hours:minutes:seconds).
How can I do it?

If you could convert to a string you can use
String.format();
for this.

The SimpleDateFormat class is within Java's utility library and it is what most people use:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Time {
public static void main(String[] args) {
//instance of the time, date
Date date = new Date();
//format it however you like.
String strDateFormat = "HH:mm:ss";
//The SimpleDateFormat class allows the actual time format
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
//use the sdf object to format
System.out.println(sdf.format(date));
}
}

Related

How can I convert a String formatted in this way into a Date using Java? [duplicate]

This question already has answers here:
Parse CIM_DateTime with milliseconds to Java Date
(2 answers)
Closed 2 years ago.
Into a Java application I have this String representing a timestamp containing values like this: 2009-10-17 05:45:14.000
As you can see the string represents the year, the month, the day, the hour, the minute, the second and the millisencond.
I have to convert a String like this into a Date object (if possible bringing also the millisecond information, is it possible?)
How can I correctly implement it?
You can use SimpleDateFormat to parse a given string date according to a given pattern, it also supports milliseconds, like this:
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date=format.parse("2009-10-17 05:45:14.050");
Since Java 8, you should use the classes in the date-time API
Class LocalDateTime stores a date and a time up to nanosecond precision.
Here is a snippet showing how to parse a string into an instance of LocalDateTime
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class DateTime {
public static void main(String[] args) {
String str = "2009-10-17 05:45:14.000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(str, formatter);
}
}

Unable to Convert String to localDate with custom pattern [duplicate]

This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Closed 2 years ago.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.time.*;
import java.time.format.*;
import java.text.*;
public class ConvertStringToDate {
public static void main(String[] args)throws Exception {
String date = "2020-06-14";
DateTimeFormatter Stringformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, Stringformatter);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String formattedDate = localDate.format(formatter); // output here is as expected 14.06.2020
// facing issues when converting back to localDate with defined pattern,
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter); // expected output is 14.06.2020 but getting a LocalDate formatted 2020-06-14
// System.out.println(parsedDate);
// System.out.println(parsedDate.getClass().getName());
}
}
Apologizes for my explanation early days with java. Basically i am trying to convert input string "2020-06-14" into a localDate with a custom pattern "dd.MM.yyyy" in the end trying to have a date object not a String. Is there an other way to achieve it.
A date has no format. Therefore when you write
//expected output is 14.06.2020 but getting a LocalDate formatted 2020-06-14
your expectation is simply wrong. The date is parsed according to the formatter but how the date represents the parsed values and how it chooses to display them afterwards no longer has any connection to the formatter.
The only way to get your format back is to write parsedDate.format(formatter) once again and you are back where you started, which is what formattedDate was already.

DateString format Conversion in Java [duplicate]

This question already has answers here:
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 5 years ago.
Can anyone tell me how to convert this dateString "2003Jan01" into another dateString which is in this format "01-JAN-03"?
One way is using two SimpleDateFormat: one to parse the input, one to format the output:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SDF
{
public static void main(String args[])
{
SimpleDateFormat source = new SimpleDateFormat("yyyyMMMdd", Locale.ENGLISH);
try
{
Date date = source.parse("2003Jan01");
SimpleDateFormat destination = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
System.out.println(destination.format(date));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Format String date to another string format using java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 8 years ago.
I want to format a from string date to another String date value. The date is show as "03/20/2014". I have to format it to "2014-03-20". Could you please give me sample codeing for this?
Thanks
import java.text.SimpleDateFormat;
import java.util.Date;
class sample
{
public static void main(String[] args) throws Exception
{
String source= new String("03/20/2014");
SimpleDateFormat sdfinput = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat sdfoutput = new SimpleDateFormat("yyyy-MM-dd");
Date inputdate=sdfinput.parse(source);
String outputDate=sdfoutput.format(inputdate);
System.out.println(outputDate);
}
}

How to format date string in java? [duplicate]

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
(1 answer)
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi i have the following string:2012-05-20T09:00:00.000Z
and i want to format it to be like 20/05/2012, 9am
How to do so in java?
Thanks
If you are looking for a solution to your particular case, it would be:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2012-05-20T09:00:00.000Z");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy, Ka").format(date);
use SimpleDateFormat to first parse() String to Date and then format() Date to String
package newpckg;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StrangeDate {
public static void main(String[] args) {
// string containing date in one format
// String strDate = "2012-05-20T09:00:00.000Z";
String strDate = "2012-05-20T09:00:00.000Z";
try {
// create SimpleDateFormat object with source string date format
SimpleDateFormat sdfSource = new SimpleDateFormat(
"yyyy-MM-dd'T'hh:mm:ss'.000Z'");
// parse the string into Date object
Date date = sdfSource.parse(strDate);
// create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat(
"dd/MM/yyyy, ha");
// parse the date into another format
strDate = sdfDestination.format(date);
System.out
.println("Date is converted from yyyy-MM-dd'T'hh:mm:ss'.000Z' format to dd/MM/yyyy, ha");
System.out.println("Converted date is : " + strDate.toLowerCase());
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
}
}

Categories