how to convert String to date using simpledateformat - java

i am converting string in to date object in android......
that string is coming from server in form of "2014-02-22" or something like that... i want to convert it in to my date which i can use in my application..
i am using Simple Date Format ,parse method to convert....
but this statement throws parse exception... meaning its not converting my string.. which is "2014-02-22"...
it should convert but its not....
so kindly help me in this..... i am getting null in response
#SuppressLint("SimpleDateFormat")
public static Date getDate(String string){
Date date = null;
try {
date = new Date();
date = new SimpleDateFormat("yyyy/MM/dd").parse(string);
}
catch (ParseException e) { e.printStackTrace(); }
catch (java.text.ParseException e) { e.printStackTrace(); }
return date;
}

Try as follows...
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date;
#SuppressLint("SimpleDateFormat")
public static Date getDate(String string){
date = new Date();
try {
date = format.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date testDate = null;
try {
testDate = sdf.parse("2013-11-12");
}
catch(Exception ex) {
ex.printStackTrace();
}
int date= testDate.getDate();
int month = testDate.getMonth();
int year = testDate.getYear();

Just use SimpleDateFormat (click the link to see all format patterns).
String string = "2014-02-22";
Date date = new SimpleDateFormat("yyy-M-d", Locale.ENGLISH).parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 BOT 2010
Here's an extract of relevance from the 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

Related

Format timestamp from database to Java

I have this weird format of date and time on my Oracle SQL Developer :
2015-4-14.1.39. 33. 870000000
I tried to format the given date to MM-dd-yyyy HH:mm:ss, but it gives me exception:
java.text.ParseException: Unparseable date: "2015-4-14.1.39. 33. 870000000"
The following is the code:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
try {
String ts = "2015-4-14.1.39. 33. 870000000";
Date date = formatter.parse(ts);
String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);
System.out.println(S);
} catch (Exception e) {
e.printStackTrace();
}
The problem is that the given date string does not match the specified format. Try use the following format:
SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd.H.m. s. S");
String ts = "2015-4-14.1.39. 33. 870000000";
df.parse(ts);
Where
yyyy for year
M for month in year
dd for day in month
H for hour in date (0-23)
m for minute in hour
s for second in minute
S for millisecond

How to convert "12-JAN-15 03.51.22.638000000 AM" in java?

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);

android change datetime format

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

How to parse date format in java? [duplicate]

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

Convert string to DateFormat

i am trying to convert a string utc date to Date. by using the following code
This is My UTC String Date - 12/31/2013 8:40:00 AM
i want to convert this string to UTC Date.
static final String DATEFORMAT = "dd/MM/yyyy HH:mm:ss aa";
StringDateToDate("**12/31/2013 8:40:00 AM**");
public static Date StringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
dateToReturn = dateFormat.parse(StrDate);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}
but i am getting the wrong date in wrong format (sun jul 12 19:40:00 CDT 2015). how can i convert this utc date string to utc date. i am getting the utcdatestring from a rest webservice in XML format.
Just try this. Probably the order of your Date Format is wrong
String dtStart = "12/31/2013 8:40:00 AM";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aa");
Date date = format.parse(dtStart);
System.out.println(date);
First your date format is wrong it should be :
static final String DATEFORMAT = "MM/dd/yyyy HH:mm:ss aa";
secondly, your input has to not have the asterixs(*) like this :
Date a = StringDateToDate("12/31/2013 8:40:00 AM");
//yea I know I should be using Log but I'm testing on java
System.out.println(a.toString());
If you really want the asterixs, do this :
String b = "**12/31/2013 8:40:00 AM**";
StringDateToDate(b.substring(2, b.length()-2));
Your input is wrong(there is no 31 month) , change it to a valid month
StringDateToDate("12/01/2013 8:40:00 AM");
to be compatible with the DateFormat
or Change your Dateformat to suit your input value
static final String DATEFORMAT = "MM/dd/yyyy HH:mm:ss aa";
Change your dateformat like this.
String DATEFORMAT = "MM/dd/yyyy HH:mm:ss aa";
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
This is the Date and Time Patterns.
String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date);
Your date format should be this in order to parse the String you have given here.
static final String DATEFORMAT = "MM/dd/yyyy HH:mm:ss aa";
And also watch out for HH. HH is Hour in day (0-23). If your input date hour is 0-11 (possibly like this since AM\PM is given and patter has aa at the end) then KK must be used instead of HH.
The result obtained is not wrong but it is what expected.
By default, parsing is lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format.
The heuristics uses the number specified for month not as months in the specified year but as months since the specified year, 31 months are 2 years and 7 months so: 01/2013 + 2years + 7 months = 07/2015.
This can be confusing so the suggestion is to set the lenient parsing to false before parsing but when pattern doesn't match something in your string a parsing exception is thrown.
public static Date StringDateToDate(String StrDate) {
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateFormat.setLenient(false);
try {
dateToReturn = dateFormat.parse(StrDate);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}

Categories

Resources