I have a set of strings. This strings are actually "year"s. for example: "1967","1872","2012",...
I want to create a SimpleDate instance from each of these. Is there a way to create a SimpleDate from a year String?
I have a rather hacky solution for this, which is attaching "-00-00" to my year strings and parsing it into a a SimpleDate. I need a more non-hacky way of doing this.
Thx
I am not sure but maybe you asking about something like this
SimpleDateFormat sdf=new SimpleDateFormat();
sdf.applyPattern("YYYY");
Date d=null;
try {
d = sdf.parse("1999");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(d);
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), 0, 1);
calendar.getTime();
You could use SimpleDateFormat.
String str_date="1997";
Date date;
try {
DateFormat formatter = new SimpleDateFormat("yyyy");
date = (Date)formatter.parse(str_date);
} catch (ParseException e) {
// handle exception (date will be null)
}
Related
I have a lot of Strings in the format shown my example that I have to parse. I'm trying to determine which of the Strings are today.
My problem is, that the time is almost there and I just need to compare that date.
Next I want to check if time is between two timestamps "HH:mm:ss" with .after and .before, but there is the problem, that the date is almost there.
How do I split that parsed format in date and time to handle each in its own way?
I'm working in Android Studio, if that's relevant.
String dtStart = "2016-05-23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
if (new Date().equals(format.parse(dtStart)) ) System.out.println("true");
else System.out.println("false");
list.add(new LatLng(lat, lng));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.time
Use the java.time classes built into Java 8 and later.
Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport, and further adapted to Android in ThreeTen-ABP.
String dateToParse = "2016-05-23 07:24:59";
LocalDateTime dateTime = LocalDateTime.parse(dateToParse, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate localDate = dateTime.toLocalDate();
LocalTime localTime = dateTime.toLocalTime();
// Compare here to your date & time
You can easily achieve it by using the SimpleDateFormat like that:
//Houres - seconds
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//Years - days
Date hoursAndMinutes = timeFormat.parse(dtStart);
Date yearsMonthsDays = dateFormat.parse(dtStart);
That way, you only get the hours, minutes and seconds of your date.
Then, you can do the same for just the year and month and compare it afterwards.
And just to be complete, here's how you'd do it using the Joda date time library and the toLocalDate() and toLocalTime() method.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime today = new DateTime();
DateTime start = formatter.parseDateTime(dtStart);
if (today.toLocalDate().compareTo(start.toLocalDate()) != 0) {
System.out.println("true");
} else {
System.out.println("false");
}
if (today.toLocalTime().compareTo(start.toLocalTime()) > 0) {
...
}
thx for help and sry i forgot to say i'm on android studio..
i found my solution here: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
String dtStart = "2016/05/23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date dtStartOK = format.parse(dtStart);
String stringDate = DateFormat.getDateTimeInstance().format(dtStartOK);
System.out.println(stringDate);
System.out.println(DateFormat.getDateInstance().format(dtStartOK));
System.out.println(DateFormat.getTimeInstance().format(dtStartOK));
} catch (ParseException e) {
//Handle exception here, most of the time you will just log it.
e.printStackTrace();
}
gives me:
23.05.2016 07:24:59
23.05.2016
07:24:59
I am trying to convert a date string of format dd.MM.YYYY into date object as following:
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.YYYY");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
But I am getting this result Date is : Sun Dec 26 00:00:00 MST 2010
I tried it in different ides and even on compileonline.com , still same result.
So Am I doing anything wrong here, because its not suppose to behave like this.
Please help.
The pattern for year is incorrect. You need to say:
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
The correct representation is yyyy
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = null;
try {
date = (Date)formatter.parse(start_dt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("Date is : "+date);
public static void main(String[] args) {
try {
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
} catch (ParseException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
you should learn about DateFormat from here link1 and link2, you will realize that your code should be like that (year should be written in small letters).
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
I get this date as a string from SOAP message
"2009-12-02T12:58:38.415+01:00"
Most i could i could identify and vary on subject was
to play with this format yyyy-MM-dd ? hh:mm:ss.????
I tried different combinations using SSS T z Z instead of '?"
DateFormat df = new SimpleDateFormat("... various formats ...");
System.out.println(df.parse("2009-12-02T12:58:38.415+01:00"));
but no success.
Any idea ?
Thanks
you have to change the timezone part. try this:
String a = "2009-12-02T12:58:38.415+01:00";
a = a.replaceFirst(":(?=\\d+$)", "");
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(df.parse(a));
I don't think this is possible using the standard Java date formatting.
Using joda-time this can be done using the following code:
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder()
.appendYear(4, 4).appendLiteral('-').appendMonthOfYear(2).appendLiteral('-').appendDayOfMonth(2)
.appendLiteral('T')
.appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':').appendSecondOfMinute(2)
.appendLiteral('.').appendMillisOfSecond(3).appendTimeZoneOffset(null, true, 2, 2);
DateTimeFormatter dateTimeParser = b.toFormatter();
System.out.println(dateTimeParser.parseDateTime("2009-12-02T12:58:38.415+01:00"));
Try with the following.
String date = "2009-12-02T12:58:38.415+01:00";
int lastIndexOf = date.lastIndexOf(":");
if(lastIndexOf>=0){
date = date.substring(0,lastIndexOf)+date.substring(lastIndexOf+1);
}
System.out.println("~~~~~~date~~~~~"+date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSZ");
try {
sdf.parse(date);
System.out.println("....date..."+date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String myDate = new String("2011-06-23T00:00:00");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
this.thedate = format.parse(myDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm not sure what I'm doing, but I have a timestamp that will be a string and I want to parse out the year and month. This is what I have so far.
You need to use "yyyy-MM-dd'T'HH:mm:ss" as a pattern for the SimpleDateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I guess format should be this:
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I have a date in(string) in dd-mon-yyyy format and I want to compare this date with system date.
eg.
I have 12-OCT-2010
and I want to compere this with system date in same format
You can use the SystemDateFormat class to parse your String, for example
final DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
final Date input = fmt.parse("12-OCT-2010");
if (input.before(new Date()) {
// do something
}
Note that SimpleDateFormat is not threadsafe, so needs to be wrapped in a ThreadLocal if you have more than one thread accessing your code.
You may also be interested in Joda, which provides a better date API
Use SimpleDateFormat http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
String d = "12-OCT-2010";
try {
Date formatted = f.parse(d);
Date sysDate = new Date();
System.out.println(formatted);
System.out.println(sysDate);
if(formatted.before(sysDate)){
System.out.println("Formatted Date is older");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I would recommend using Joda Time. You can parse that String into a LocalDate object very simply, and then construct another LocalDate from the system clock. You can then compare these dates.
Using simpledateformat -
String df = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(df);
Calendar cal = Calendar.getInstance();
/* system date */
String systemdate = sdf.format(cal.getTime());
/* the date you want to compare in string format */
String yourdate = "12-Oct-2010";
Date ydate = null;
try {
ydate = sdf.parse(yourdate);
} catch (ParseException e) {
e.printStackTrace();
}
yourdate = sdf.format(ydate);
System.out.println(systemdate.equals(yourdate) ? "true" : "false");