Android 4.3 jelly Bean Time format issue - java

My date format is this "yyyy-MM-dd" and when i get month using this function it return me wrong format of month. For example instead of "July" it return me only "J"
here is the function:
public static String getMonthName(String date) {
Date mDate = Utils.parseDate(date);
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM");
String time = sdf.format(mDate);
return time;
}
andy idea what to do?
Edit:
and here is my parseDate(String date) function
public static Date parseDate(String date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}

I think you have one M to much
M:1
MM:01
MMM:Jan
MMMM:January
MMMMM:J

Related

error: Cannot format given Object as a Date, when formatting date to new format (Android)

I want to change the format of dates that stored in a list. And then save that list of date to firebase database. But I got error when formatting the date that stored in dateList.
This is my code :
MainActivity.java
List<Date> dates = getDates(date1, date2);
ArrayList<String> dateStringList = new ArrayList<>();
ArrayList<Date> dateList = new ArrayList<Date>();
SimpleDateFormat format1 =new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
for (Date date : dates) {
String dateStr = String.valueOf(date);
dateStringList.add(dateStr);
}
for (String dateString : dateStringList) {
try {
dateList.add(format1.parse(dateString));
format2.format(dateList); ---> error here when formatting the date to format2
} catch (ParseException e) {
e.printStackTrace();
}
}
String date1 = format2.format(dateList);
dateModel data = new dateModel(dateList);
mReference.child(id).setValue(data);
dateModel.java
public class dateModel {
ArrayList<Date> date;
public dateModel() {}
public dateModel(ArrayList<Date> date) {
this.date = date;
}
public ArrayList<Date> getDate() {
return date;
}
public void setDate(ArrayList<Date> date) {
this.date = date;
}
}
is there a way to solved this?
thank you so much..
If I understand correctly, your code doesn't even compile.
Check your types.
The format method takes a Date, not a List and I don't see why you need two formats
List<Date> dates = getDates(date1, date2);
ArrayList<String> dateStringList = new ArrayList<>();
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
for (Date date : dates) {
try {
dateStringList.add(fmt.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
dateModel data = new dateModel(dateList);
mReference.child(id).setValue(data);
Note that saving your list as strings probably isn't a good idea other than "visual inspection". It's the UI responsibility to format dates and times , in my opinion, and storing milliseconds (date.getTime()) is easier to query and filter in certain cases.
datelist is an ArrayList. You cannot format an arraylist to a date.
It should be
format2.format(dateString)

Java - How to check if a value is of type Date

I have a project requirement. There are values in a .txt as -
02/01/2017 00:00:00
Now I need to have some rules to check if this value in the data file is of type Date. How can I do that? Thanks. I am new to Java so any help much appreciated. Thanks
Try to parse it to date. If it throws ParseException then it is not a date.
String dateString = "02/01/2017 00:00:00";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Date date;
try {
date = df.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
If you just want valid days, use non-lenient parsing:
String dateString = "02/28/2017 00:00:00";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
df.setLenient(false);
Date date;
try {
date = df.parse(dateString);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
This will throw an exception for non-existing days like the 31st of February
Methord 1
Date dt = new Date(); //this should your Dynamic object
if (dt.getClass().equals(new Date().getClass()))
{
//if its date object
}
Methord 2
if(dt instanceof Date){
//if its date object
}
Use this to check date
String sDate1="31/12/1998 00:00:00";
try{
Date date1=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse(sDate1); `
}
catch(Exception e)
{
//Not a date..
}
You can use regular expressions to check the format
public boolean isDate(String s){
String pattern= "([0-9]{2})/([0-9]{2})/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})";
return s.matches(pattern);}
Here’s the Java 8 solution (it can be made to work in Java 6 and 7 too when you use the ThreeTen Backport).
private static final DateTimeFormatter DATE_TIME_FORMAT
= DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss")
.withResolverStyle(ResolverStyle.STRICT);
public static boolean hasTypeDate(String stringFromTxt) {
try {
DATE_TIME_FORMAT.parse(stringFromTxt);
return true;
} catch (DateTimeParseException dtpe) {
return false;
}
}
Stay away from SimpleDateFormat for new code. It has had its time, it’s been outdated for a while now.
If you intended the day of month first (if 02/01/2017 means January 2nd), you need the format pattern dd/MM/uuuu HH:mm:ss instead.
If you need to know which date and time was in the .txt, use:
LocalDateTime dateTime = LocalDateTime.parse(stringFromTxt, DATE_TIME_FORMAT);
Link: ThreeTen Backport: java.time classes for Java 6 and 7.
public static boolean isValidDate(String inDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
public static void main(String[] args) {
String file = "/path/to/your/file.txt";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = br.readLine()) != null) {
// do something with line
}
br.close();
isValidDate(line));
//do what you want :)
}

Having difficulty parsing date and time together Android

I need help parsing this string of data and time. Could any one help me fix my format or give guidance? Thanks.
2016-04-23T01:00:00Z
SimpleDateFormat year = new SimpleDateFormat("mm:dd:yyyy");
SimpleDateFormat hour = new SimpleDateFormat("hh:mm:ss");
String yearParse = year.format(Date.parse(year));
String hourParse = hour.format(Date.parse(hour));
Use this methods to get Date and Time in format you wants
For Date
public static String getDate(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
return formatter.format(parsed);
}
For Time
public static String getTime(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(parsed);
}
Try
SimpleDateFormat sdf1= new SimpleDateFormat("mm-dd-yyyy");
SimpleDateFormat sdf2= new SimpleDateFormat("hh-mm-ss");
String year=sdf1.format(required_date);
String hour=sdf2.format(required_date);

how to convert string into date? JAVA

I have this string: 7 -Jun- 2014.
I want to convert to java.utils.Date;
I use this Code
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
but I get this exception :
java.text.ParseException: Unparseable date: "7-Jun-2013"
at java.text.DateFormat.parse(Unknown Source)
at ma.abcsolution.util.Test.main(Test.java:15)
try using
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
the SimpleDateFormat look at the given string like it you tell it to, so in your example it look for a string with 2 chars for days, followed by a '/' sign and then 2 chars for month and so on
Using SimpleDateFormatter you can convert from date string to date and date to date String
public final class DateUtil {
private static final String DEFAULT_DATE_FORMAT = "dd-MMM-yyyy";
private DateUtil() {
}
public static final String formatDate(final Date date, final String dateFormat) {
DateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
return format.format(date);
}
public static final Date formatDate(final String date) throws ParseException {
return formatDate(date, DEFAULT_DATE_FORMAT);
}
public static final Date formatDate(final String date, final String dateFormat) throws ParseException {
DateFormat format = new SimpleDateFormat(dateFormat);
return format.parse(date);
}
}
Whenever you are going yo format date please verify the format you are using
In your case you used dd/MM/yyyy but date you used in format dd-MMM-yyyy.
Use
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
to format the date. You used the wrong format with the / signs.
See also:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Date date = new SimpleDateFormat("d-MMM-yyyy").parse("7-Jun-2014");
Use this code. It is simple.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
public static void main(String args[]){
SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
Date date=new Date(dateInString);
System.out.println(ft.format(date));
}
}

parsing only the Year in a DateFormat Java

I get a returned parsed JSON result with string values in the form of dates like "27-11-2012" which i parse to a date Object. my code for this is:
public Date stringToDateReport(String s){
//Log.d(TAG, "StringToDateReport here is " + s);
DateFormat format;
Date date = null;
//if(s.matches(""))
format = new SimpleDateFormat("dd-MMM-yyyy");
try {
date = (Date)format.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
now my issue is, a feature has been implemented that sometimes the json only returns a year object like "2012" and is giving me an "ParseException: Unparseable date" as expected. I was thinking of using regex to match the string pattern and parse from there, but not sure how to do that. Any ideas and also anyway to parse only year in a DateFormat?
I'd try:
public Date stringToDateReport(String s){
DateFormat format;
Date date = null;
format = new SimpleDateFormat("dd-MM-yyyy");
if(s.length()==4) {
format = new SimpleDateFormat("yyyy");
}
try {
date = (Date)format.parse(s);
} catch (ParseException e) {
//you should do a real logging here
e.printStackTrace();
}
return date;
}
The logic behind is to check if the string is only 4 long, then apply the different format. In this case, this easy method is sufficient, but in other methods, the use of regexes might be required.
Try this code
public Date stringToDateReport(String s){
//Log.d(TAG, "StringToDateReport here is " + s);
DateFormat format;
Date date = null;
if(s.indexOf("-") < 0){
format = new SimpleDateFormat("yyyy");
}else{
format = new SimpleDateFormat("dd-MMM-yyyy");
}
try {
date = (Date)format.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Is there the possibility in have another format in the String s ? Or just these two?
public Date stringToDateReport(String strDate){
DateFormat formatnew SimpleDateFormat("dd-MM-yyyy");
Date date = null;
if(strDate.length()==4) {
format = new SimpleDateFormat("yyyy");
}
try {
date = (Date)format.parse(strDate);
} catch (ParseException e) {
//error parsing date
e.printStackTrace();
}
return date;
}
then call it like this :
String strDate = yourJson.getString("date");
Date d = stringToDateReport(strDate);

Categories