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();
}
}
}
Related
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));
}
}
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
return date type with format in java [duplicate]
(1 answer)
Closed 4 years ago.
I want to take a date value as String in the format yyyy-MM-dd and return a jabva util date in the same format.
for this I am using below code , but the result is coming as "Wed Sep 18 00:00:00 CEST 2013"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strdate = "2013-09-18";
Date utilDate = sdf.parse(strdate);
System.out.println(utilDate);
Do i need to consider the locale also ?
Please help to achieve this.
Thanks in advance
Please try below code.
You also have to format your simpleDateFormat object and pass utilDate object inside it to create formatted date as specified by you.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strdate = "2013-09-18";
Date utilDate = sdf.parse(strdate);
String date=sdf.format(utilDate );
System.out.println(date);
}
}
We can also implement through Java8 like below:
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTest {
public static void main(String[] args) throws ParseException{
String strdate = "2013-09-18";
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(strdate, formatter);
System.out.println(date.format(formatter));
}
}
or you can also use only LocalDate to change in java util like below.
import java.time.LocalDate;
public class TestCircle {
public static void main(String args[]){
String strdate = "2013-09-18";
LocalDate date = LocalDate.parse(strdate);
System.out.println(date);
}
}
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
How to convert string "Monday, 10 January 2017 | 04:58 PM" to a java.sql.Date 2017-01-10?
Use SimpleDateFormat:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Main {
public static void main(String[] args) {
try {
SimpleDateFormat format = new SimpleDateFormat("EEEEEE, dd MMMMMMM yyyy | hh:mm a");
Date parsed = format.parse("Monday, 10 January 2017 | 04:58 PM");
java.sql.Date sql = new java.sql.Date(parsed.getTime());
} catch (ParseException e){
e.printStackTrace();
}
}
}
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);
}
}
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);
}
}
}