How to save date (dd.mm.yyyy) in java - java

I am getting a date in the form of dd.mm.yyyy and want to save it as a proper object. It should be comparable to another date object. How do I realize this?

Use SimpleDateFormat as this:
Date date = new SimpleDateFormat("dd/MM/yyyy").parse("12/05/2015");

Using JodaTime
String input = "03.01.2015";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");
DateTime dt = DateTime.parse(input, formatter);
DateTime now = new DateTime();
System.out.println(dt.compareTo(now));

Use SimleDateFormat
String string = "03.01.2015";
DateFormat format = new SimpleDateFormat("MM.dd.yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);

Try this code
try {
SimpleDateFormat sdf= new SimpleDateFormat("dd.MM.yyyy");
Date d = sdf.parse("19.05.2090");
System.out.println(d);
} catch (ParseException ex) {
ex.printStackTrace();
}

Related

Convert only time part in android from string to time

I need to convert a time stamp that currently is in string format "08.00" to a valid time in java so I later can compare time. How do I convert this string to time?
Something like this
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date date = sdf.parse(time);
Instead of using the Date and/or SimpleDateFormat classes, perhaps consider LocalTime
String time = "08:00";
LocalTime lt = LocalTime.parse(time);
System.out.println(lt);
Output:
08:00
And can compare to other times easily with LocalTime::isBefore() or LocalTime::isAfter()
Example
Try below code,
String time = "08.00";
try {
DateFormat sdfInput = new SimpleDateFormat("hh.mm");
Date date = sdfInput.parse(time);
DateFormat sdfOutput = new SimpleDateFormat("hh:mm");
Log.e( "Time: ", sdfOutput.format(date));
} catch (Exception e) {
e.printStackTrace();
}
Output -> Time: 08:00
The easiest way to do so is with a SimplDateFormatter:
DateFormat sdf = new SimpleDateFormat("hh:mm")
Then you can call Date date = sdf.parse(*your time string*)
Now you have your time as a valid Date object.
I have timeformat like this hhmmss="151918"
so you can use any format instead of hhmmss according to your current time format like
"hh.mm" or hh:mm:ss etc
and you can call this method form any where you needed.
fun convertTimeFormat(time:String):String{
var formattedTime=""
try {
val inputFormat: DateFormat = SimpleDateFormat("hhmmss")
val timeObj = inputFormat.parse(time)
Log.d("timeObj",""+timeObj)
formattedTime=SimpleDateFormat("hh:mm aa").format(timeObj)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedTime
}

java.text.ParseException: Unparseable date: "2018-05-23T06:39:37+0000"

I'm trying to create a Date from a String I receive from the server. The String is:
2018-05-23T06:39:37+0000
So the correct format should be:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Here is my code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
I don't know if there is any way to do this, because after that I would like to parse again to the next format:
dd/MM/yyyy hh:mm
I've tried to parse the original String using this last format directly but I'm getting the same exception.
Any suggestion?
I see you've solved your own problem with a little help from the comments, however I would suggest you seriously consider LocalDate, as the older Date classes are quite troublesome at times.
In fact, as your incoming value has a TimeZone, you'll need to use ZonedDateTime to parse your input.
String createdDate = "2018-05-23T06:39:37+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime localDate = ZonedDateTime.parse(createdDate, formatter);
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
Output:
23/05/2018 06:39
The given input date String format
2018-05-23T06:39:37+0000
is incorrect so that you are getting ParseException since millisecond(SSS) part is missing from your date format yyyy-MM-dd'T'HH:mm:ss.SSSZ
So please try with
2018-05-23T06:39:37.235-0530
so below code should work
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
System.out.println(parsedDate.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
Ok, the first mistake (as you've pointed) is I didn't have milliseconds on the original String.
After removing "SSS" from the simpleDateFormat it works like a charm. So this is the final code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
try {
Date parsedDate = defaultDateFormat.parse(createdDate);
SimpleDateFormat finalDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
createdDate = finalDateFormat.format(parsedDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);

How to get proper written date using SimpleDateFormat in Java

I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".
If I use the code above I end up with a Date object, but all the methods are deprecated. So how do I achieve this?
You can use DateFormat again as #Thomas wrote:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.
Use this to get Date from your required pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);

Converting date to long in Java

I have the following string 02/11/2012 23:11
Is there any way to convert this to the long 021120122311l?
I am trying to map objects to a specific position in an array based on their date created.
You can try this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date inputDate;
try {
inputDate = simpleDateFormat.parse("02/11/2012 23:11");
System.out.println(inputDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
ouput:
1351878060000
you can use a SimpleDateFormat to generate it as a string without any symbol, and then parse the long value using the Long class
SimpleDateFormat s1 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat s2 = new SimpleDateFormat("ddMMyyyyHHmm");
Date d = s1.parse("02/11/2012 23:11");
String s3 = s2.format(d);
System.out.println(s3);
long l = Long.parseLong(s3);
System.out.println(l);
Try this,
String dateValue = "02/11/2012 23:11";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = simpleDateFormat.parse(dateValue);
simpleDateFormat = new SimpleDateFormat("ddMMyyyyHHmm");
String value = simpleDateFormat.format(date);
Long result = Long.parseLong(value);
System.out.println("Result : "+result);
Time zone is critical to parsing a string into a date-time value unless you absolutely sure all the strings represent date-times occurring in the same time zone.
Joda-Time makes this work much easier.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd/MM/yyyy hh:mm" );
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = formatter.withZone( timeZone ).parseDateTime( myString );
long millisecondsSinceEpoch = dateTime.getMillis();

date conversion issue in java

I accept the user input date in yyyy/mm/dd format. When I try to convert using SimpleDateFormat it shows "null";
my code for conversion is:
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/mm/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate);
why is it showing null?
MM not mm,
MM is for months,
mm is for minutes
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date dateOfBirth = dateofbirthFormat.parse("1986/12/11");
System.out.println(dateOfBirth);
} catch (ParseException e) {
}
By yyyy/mm/dd I guest you mean years/Months/days. So you may try using
yyyy/MM/dd (note upcase use of MM).
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate); .

Categories