This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below
Related
This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I really need your help. I have searched and tried every example I could use, but none have worked.
I need to store current date in YYYY-MM-DD format in a text file..the String date has to be a string..
String dateF = "YYYY-MM-DD";
Date dateOnly = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateF);
String date = dateFormat.format(dateOnly);
when I tried that code above.. this the output I got
please|work|2016-04-110|11
please help me...this is my assignment due this Friday ): I just need this date and 2 other things to be done..
thanks :)
your issue comes from the case you used for Y and D. according to the API SimpleDateFormat documentation, you should use d (day in month) instead of D (day in year), in your format definition.
String dateF = "yyyy-MM-dd";
Format being used is incorrect.
YYYY-MM-DD : Capital DD will return Day in the year. So, 11 April corresponds to 110th day in the year.
yyyy-MM-dd : Small dd will return the Day in the month.
Refer: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this code :
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(c.getTime());
The formattedDate contain 2016-04-19
Edit :
In your code change the YYYY to yyyy and DD to dd.
This question already has answers here:
Simple date formatting giving wrong time
(4 answers)
Closed 7 years ago.
I am trying to utilise the Calendar apart from implementing my own logic.
I am setting the Calendar value and trying to get the time in a format, below is the code
String timeValue = "06/11/2015 06:30 pm";
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy HH:mm a");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse(timeValue));
Logger.d(TAG, "Hour is = " + calendar.get(Calendar.HOUR));
SimpleDateFormat slotTime = new SimpleDateFormat("hh:mma");
SimpleDateFormat slotDate = new SimpleDateFormat(", dd/MM/yy");
Logger.d(TAG, " Date = " + slotDate.format(calendar.getTime()) + " Time is = " + slotTime.format(calendar.getTime()));
}catch (ParseException parseEx){
parseEx.printStackTrace();
}
I am expecting slotTime.format(calendar.getTime())) should return 6.30 PM while it is returning 12.30 AM.
How can I get the desired output which is 6.30 PM , What mistake I am doing
Your code is OK. The mistake is on the datetime mask:
The ".SSS" field is too much. This is only to expect for milliseconds, and, as far as I can see, you do not expect milliseconds in your input string.
The "HH" mask should be "hh" for 1-12 hours format.
Thus, let it be:
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm a");
You have to remove milliseconds from your Simple Date Format (SSS).
I get a java.text.ParseException running your code.
Try using a Simple Date Format string of "dd/MM/yyyy HH:mm a"
you have an error with the String in the time format
String timeValue = "06/11/2015 06:30 pm";
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm a.SSS");
a.SSS // .SSS is for Millisenconds which is not correct in the String you are trying to parse.
I removed it and worked fine for me.
Take a look at DateFormat.getTimeInstance(), DateFormat.getDateInstance() and DateFormat.getDateTimeInstance() as these methods return a DateFormat which will honor the users local settings (e.g. 12/24 hour system or date formats like 2016/01/01 or 01.01.2016). This is very important if you plan to release your app in multiple languages. Note that these methods also take a int as parameter with which you can style the resulting format (e.g. short format).
See here for more details.
A complete example would llok like this (creates a String like 3:04 PM on devices with English language and 15:04 on devices with e.g. German language):
String s = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(new Date()); // Creates a String like 3:04 PM
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-
Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);
dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.
I want something similar which will return formatted date in Date data type.
That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.
Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.
Given your example code, it would look like this:
String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);
This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 1 year ago.
I have a date and time picker and I build a string date using these two. Here's a sample date and time string.
"11/6/2013 09:23"
Now I need to convert them into a date and convert them to this format "yyyy-MM-dd'T'HH:mm:ss.SSS".
My problem is I'm having this error in my logcat:
11-06 21:23:53.060: E/Error(26255): Unparseable date: "11/6/2013 09:23" (at offset 2)
I'm using this code to do the conversion of string to date, and the date to a formatted string.
Date d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = d.toString();
When I use new Date and get the current date, it works fine. I guess the format of my string is wrong. But I'm displaying it on an edittext in that format. I want to stay it in that way. Is there anyway to convert this string format to a date? Any ideas guys? Thanks!
I think you are trying to do 2 things at once. In order to convert any String to another String in a different format you need to:
Parse the string with a DateFormat containing the current format, this returns a Date; then
take your newly obtained Date and format it under the desired DateFormat
SimpleDateFormat formatOne = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = formatOne.parse(etaToDeliverArrivedAtShipper.getText().toString());
SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
String result = formatTwo.format(date)
you are trying to parse in yyyy-MM-dd'T'HH:mm:ss.SSS but it is not. it is probably yyyy/M/dd HH:mm:ss
What you probably want is:
Date d = new SimpleDateFormat("yyyy/M/dd HH:mm:ss", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).format(d);
This is simply because your pattern is not matching the date string you are trying to parse. There are 3 errors in your pattern :
You use MM which means the month should be on two digits, while in the date it is only 1 digit
You use .SSS which means there are milliseconds but your date is not that precise
You are using the wrong delimiter
So the right pattern should be : yyyy/M/dd HH:mm:ss
To get the desired format, then create a new SimpleDateFormat object with the desired pattern and use the parse(Date) method giving it the Date object previously returned by parse().
This question already has answers here:
getting month name wrong with SimpleDateFormat
(2 answers)
Closed 9 years ago.
I want a timestamp field. I tried the below to get time in mmddyyyyhhmmss format.
All the fields give proper data but the month field has improper value.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mmddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
O/P I get is 13132013021331. What I expect is 03133013021331.
Your Month should be MM not mm
mm means Minutes
SimpleDateFormat mmddyyyy returns improper data for month
coz your format for month is wrong.
it should be:
new SimpleDateFormat("MMddyyyyhhmmss");
MM---> Month
mm---> minutes
You need this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
You used "mm" twice - once for month (incorrect) and again for minutes (correct).