This will probably be a dumb question, but I don't understand the java date function. Here is some code:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date s = sdf.parse(var);
Calendar scal = java.util.GregorianCalendar.getInstance();
scal.setTime(s);
Log.w("Time: ", Long.toString(s.getTime()));
If var = "10:00" I get "64800000".
If var = "11:00" I get "68400000".
If var = "12:00" I get "28800000".
If var = "13:00" I get "75600000".
If var = "14:00" I get "79200000".
If var = "00:00" I get "28800000".
What is up with 12:00? Why, when var=12:00 do I get the same result as when it's 00:00? All the other results seem correct. I obviously don't understand the java date function, but I can't seem to find any explanation for this anywhere. This is screwing up my time span calculator.
If you want to use 24-hour time, you need to use the capital HH format:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
I should have used "H" instead of "h". "h" is for am/pm format.
Related
I am having trouble parsing a string to a calendar object in java by pulling it from an XML element (I'm writing an android app) so I can calculate the length of time between the time in the string and the current time. I am brand new to java and come from the .NET world, so any additional background here can be really helpful.
The code I am trying to use is:
// Using this class to define the format for date parsing.
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd HH:mm:ss", Locale.ENGLISH);
// readArrivalValue reads the element from XML, which seems to be working.
String prdt = readArrivalValue(parser, "prdt");
// At this point prdt evaluates to the correct value: "20150331 20:14:40"
Date datePRDT = sdf.parse(prdt);
// datePRDT doesn't seem to be set properly. It evaluates to:
// datePRDT = {java.util.Date#830031077456}"Sat Jan 31 20:14:40 CST 2015"
// milliseconds = 1422761216000
// element.prdt is of type Calendar. Nothing is done to this prior to this statement.
element.prdt.setTime(datePRDT); // This throws a null pointer exception
Two issues with your code:
Fix your SimpleDateFormat pattern as you're parsing minutes mm in place of months MM too!
new SimpleDateFormat("yyyyMMdd HH:mm:ss", Locale.ENGLISH);
^
You most likely forgot to initialize your Calendar instance and hence get an NPE.
element.prdt = Calendar.getInstance();
element.prdt.setTime(datePRDT);
I am working on android app with achartengine where I am making a TimeSeries linegraph. I have stored all my variables inside an Arraylist. Since I need correct date object to insert in the time axis of my chart I am using,
int count = list.size();
Date[] dt = new Date[count];
for(int i=0;i<count;i++){
long a = Long.parseLong(list.get(i).get("time"));
dt[i] = new Date(a);
}
Here long a has the timestamp . With above piece of code. I am able to get dt as 09-Apr-2014 but I need the date to be shown as 09-Apr 12:55 . How can I do that,
I tried using the folllowing
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd HH:mm");
Date now = new Date();
String strDate = sdfDate.format(now);
But Since strDate is a string I cannot use it as dt[i] = strDate which will throws an error as one is Date and another is String.
How can I solve this ?
Thanks
You can solve it this way:
dt[i] = sdfDate.parse(strDate);
If you really just need the date strings, you can do this:
int count = list.size();
String[] dt = new String[count];
for (int i = 0; i < count; i++) {
long a = Long.parseLong(list.get(i).get("time"));
Date d = new Date(a);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
dt[i] = dateFormat.format(d);
}
Or, if you actually need the Date array, just format the dates on the fly as you need them.
Your question is misguided - you are showing how you create Date objects in the code, yet what you want to fix is how you show them.
The Date array will have dates precisely to the millisecond. The default toString() method of the Date objects shows only the day, that's why you're not seeing the time.
It is inherently the UIs responsibility to decide on the format of time that it is going to print, hence you should pass the Date array to the UI (or up to the point of printing) and format them there.
The DateFormat can do both (date to string representation and back):
DateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" );
Date to String:
Date date = new Date();
String sDate = formatter.format( time );
String to Date:
Date date = formatter.parse(sDate );
When you store the date, you should store it as precise as possible (milliseconds). For displaying it as a string, you can use whatever format you want.
Currently, both display language and format language of Windows 7 are English. So the short time format is "h:mm tt"
Run the following code:
Date today = Calendar.getInstance().getTime();
DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
String dateOut = timeFormatter.format(today);
System.out.println("short: " + dateOut);
Get the result:
short: 10:36 AM
Then change the short time format to "H:mm", run the code again, still get the result:
short: 10:36 AM
But what I expect is
short: 10:36
And I don't want to hard code the format pattern, it should be following the short time format changes.
Does anyone have any idea? Thanks a lot.
Try it:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
String dateOut = dateFormat.format(today);
System.out.println("short: " + dateOut);
I need to convert this 1384174174 timestamp from PHP to Java. This is how I echo the date('Y/m/d H:i:s' ,$dn1['timestamp'] in PHP yet I don't know how to do it in Java. Please help me. Thanks.
In Java, you'd do it like this:
Date date = new Date(1384174174);
SimpleDateFormat f = new SimpleDateFormat("Y/M/d H:m:s");
String dateFormatted = f.format(date);
Watch out for the format pattern where, unlike PHP, M is month in year and m is minute in hour.
This method will take your Unix style date and create a Java Date. It returns a readable string. It should help you get started.
private String unixToString(long unixSeconds) {
Date date = new Date(unixSeconds);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
I have the following problem and I couldn't find the best solution for it yet.
Lets say I have an integer with the following value:
int miliseconds = 65111;
I want to print it to an stream using the printf() function. Is there a way I can do the following:
printf("Time: %uu:mm:ssT", miliseconds");
so it will return:
Time: 00:01:05
Here I just made up the %uu:mm:ssT part, but is there a way to do this.
Also, do you know a website where I can find all the formatting options, so I can look it up myself next time.
Use
final int miliseconds = 65111;
System.out.printf("%1$TM:%1$TS.%1$TL\n", (long) miliseconds);
and see Format String Syntax for details.
Have a look at the SimpleDateFormat feature.
Consider the following sample
String inputPattern = ....
String outputPattern = ....
String ms = ((Integer) milliseconds).toString();
DateFormat inFormat = new SimpleDateFormat(inputPattern);
DateFormat outFormat = new SimpleDateFormat(outputPattern);
// Parse input
Date date = inFormat.parse(ms);
// Format the output
String output = outFormat.format(date);
[Edit] - Updated sample to only use j2se instead of including yodatime.
If you use the SimpleDateFormat to format a time from new Date(milliseconds), don't forget that SimpleDateFormat is time zone sensitive. So set it to UTC before using, like this:
DateFormat outFormat = new SimpleDateFormat("HH:mm:ss");
outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = new Date(milliseconds);
String result = outFormat.format(d);
(I'm using a similar code in my program for a JSpinner to input a millisecond time value.)