I have date-time in format 05.12.2014 16:43:43 and I am trying to change its format to this MM/dd/yyyy. This is my source:
String mydate = "05.12.2014 16:43:43";
SimpleDateFormat srcDf = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
try {
Date date = srcDf.parse(mydate);
SimpleDateFormat destDf = new SimpleDateFormat("MM/dd/yyyy");
mydate = destDf.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
I have an error in first SimpleDateFormat:
SimpleDateFormat srcDf = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
How can I solve my problem?
Change to
SimpleDateFormat srcDf = new SimpleDateFormat(
"dd.MM.yyyy HH:mm:ss");
So that is match your String.
The symbols are explained here
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Letter Date or Time Component Presentation ExamplesG Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
Related
How to support more than one datetime pattern like yyyyMMdd and yyyy-MM-dd?
With SimpleDateFormat, you can set your own date patterns. For example, dd/mm/yyyy, mm/dd/yyyy, yyyy-mm-dd, and so on.
The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
(from Java API doc)
Source
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
public static void main(String[] args) {
String pattern = "MM/dd/yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("12/31/2006");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
// formatting
System.out.println(format.format(new Date()));
}
}
Output
Sun Dec 31 00:00:00 PST 2006
01/26/2007
I am working on date format with SimpleDateFormat. But when I format my date in provide format, year value increment by 1.The condition happen with that value. Why this happen?
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
System.out.println("dob - "+date); // Java date
System.out.println("formatted date - "+sdf.format(date));
Output as follows :
dob - Sun Dec 28 00:00:00 IST 1975
formatted date - 28-12-1976
Year value increment by 1. Why this happen.
As per Oracle docs
capital Y means week year,you should use yyyy in place of YYYY
change SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY"); to
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
You should use SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
as y means year while 'Y' means week Year.
Below are supported codes to be used in SimpleDateFormat.
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
Try to use "yyyy" instead of "YYYY".
See for more info:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
Y means "Week year"
y means "Calendar year"
I have a date in the format "12-JAN-15 03.51.22.638000000 AM".
I want it to convert to "12-01-15 00:00:00.000"
Even though there are hours,minuts and secs etc,i want the output with zeros only.
You want to convert one date format to another. This answer does exactly that. It states
DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse("August 21, 2012");
String formattedDate = targetFormat.format(date); // 20120821
In your case the original and target format are as follow
Original format: dd-MMM-yy hh.mm.ss.N a
Target format: dd-MM-yy hh:mm:ss:S
I am not sure how to replace the time data with 0. Perhaps a string manipulation is the way to go in your case. But if you want more control then you can do something like this.
Calendar cal = Calendar.getInstance();
cal.setTime(date); // this is the date we parsed above
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
formattedDate = targetFormat.format(cal.getTime());
EDIT
# Sufiyan Ghori has provided a more cleaner way to do it.
Using Java 8,
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("MM-dd-yy:hh:mm:ss:nn"); // n = nano-of-second
LocalDateTime today = LocalDateTime.of(LocalDate.of(2015, 1, 15),
LocalTime.of(00, 00, 00, 00));
System.out.println(today.format(formatter));
Output
01-15-15:12:00:00:00
Explanation,
LocalDateTime.of(LocalDate.of(int Year, int Month, int Day),
LocalTime.of(int Hour, int Minutes, int Seconds, int nanoOfSeconds));
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
You can follow to this javadoc, listing all available format patterns:
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
u Day number of week Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT- 08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
You can refer to this answer for detailed explanation.
String dateInString = "12-JAN-15 10.17.07.107000000 AM";
dateInString = dateInString.substring(0, 9);
Date date = null;
try {
date = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH).parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
String newFormat = new SimpleDateFormat("dd-MM-yy 00:00:00.000").format(date);
System.out.println(newFormat);
This question already has answers here:
Change date format in a Java string
(22 answers)
Java string to date conversion
(17 answers)
Closed 9 years ago.
java.text.ParseException: Unparseable date: "2014-01-15 00:003:00" ecxception is throwing
please shorout my peoblem
Thanks
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Dd {
public static void main(String args[]) throws ParseException {
String s1="2014-01-15 00:003:00";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy--MM-dd h:mm:ss");
try{
Date date =sdf.parse(s1);
SimpleDateFormat sdf1=new SimpleDateFormat("dd--MMM-dd H:mm:ss");
String s3=sdf1.format(date);
System.out.println(s3);
}
catch (ParseException e) {
System.out.println(e);
}
}
}
Try this..
String s1 = "2013-01-15 8:00:03";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd h:mm:ss");
try {
Date dt=sdf.parse(s1);//converting to date
SimpleDateFormat sdf1=new SimpleDateFormat("dd-MMM-yyyy h:mm:ss");
String s2=sdf1.format(dt);//formating to new format string
System.out.println(s2);
} catch (ParseException ex) {
}
//new answer
public class Dd {
public static void main(String args[]) throws ParseException {
String s1 = "2014-01-15 00:003:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date date = sdf.parse(s1);
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
String s3 = sdf1.format(date);
System.out.println(s3);
} catch (ParseException e) {
System.out.println(e);
}
}
}
You can use SimpleDateFormat
The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
Any Clues on how to fix this:
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd hh:24mi:ss");
Exception:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'i'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:696)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:515)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:464)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:445)
at CopyEJ.CopyEJ.main(CopyEJ.java:105)
Your pattern string is not following the java standard. This should work:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Your date format pattern string contains the invalid sequence 24mi.
change to:
SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
note:
the Big letter "HH" means 24 hours: e.g 23:59:00
while "hh" is 12 hours format: 11:59:00
As pointed by others, the pattern string is not correct, and can be as showed by others "yyyy/MM/dd HH:mm:ss".
Refer the java documentation for looking up the available pattern letters that could be used for formatting a date.
Snippet from the above link
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
If you want hours in 24 hour format
Use this
DateFormat df=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dd=new Date();
System.out.println(df.format(dd));