GetTime doesn't correctly display zero's - java

I use TimeStamps in my system. Everything works correctly, however I find it annoying to have [12:2:2] as TimeStamp. Because you don't know if it's 20 or 2.
I am tired and sick, and starting to think it is the compiler.
java.util.Date date= new java.util.Date();
Timestamp time = new Timestamp(date.getTime());
super.print("[" + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "] ");re
How do you guys get correct TimeStamps with the zero's?

Have a look at the DateFormat or SimpleDateFormat class.
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());

Use simple String.format with %2d flag, i.e. decimal number with at least 2 digits.
String.format("[%2d:%2d:%2d] ", time.getHours(), time.getMinutes(), time.getSeconds())

You can use the DateTimeFormatter like this:
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime = format.parseDateTime(s);
where s is your datatime string.

Related

Determine timezone from string "2016-09-14T00:00:00.000-07:00/2016-09-15T00:00:00.000-07:00"

My code is running with -Duser.timezone=UTC.
I am looking to create an Interval with the correct timezone from strings.
For example:
"2016-09-14T00:00:00.000-07:00/2016-09-15T07:00:00.000-07:00"
Interval.parse(str) should give me
[2016-09-14/2016-09-15] in PST.
But that always give me an Interval in UTC instead of PST b/c of -Duser.timezone=UTC option.
The only way I know of to solve this is tedious and involves the following steps:
Parse the string
Determine Offset
Find what timezone that offset corresponds to.
(whitelist? or someone has a list already?)
Then create a new Interval myself.
Is there a better way?
This should get you started:
String dateString = "2016-09-14T00:00:00.000-0700/2016-09-15T07:00:00.000-0700";
String datesInString[] = dateString.split("/");
SimpleDateFormat inputDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
outputDateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
Date date0 = inputDateFormat.parse(datesInString[0]);
Date date1 = inputDateFormat.parse(datesInString[1]);
String result = outputDateFormat.format(date0) + "/"
+ outputDateFormat.format(date1) + " "
+ outputDateFormat.getTimeZone().getID();
System.out.println(result);

Java new date() with milliseconds

i got this:
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date data = new Date();
how can be the "data" variable be in the "sdfDate" format?
i need about this output (it must be the current time):
2014-11-10 17:48:20.128
You should do:
sdfDate.format(data);
This is the correct way to use your dateformatter to format a given date.
Ex.
System.out.println("Date: " + sdfDate.format(data));
Use the format function on sdfDate. SimpleDateFormat inherits it from DateFormat.
String result = sdfDate.format(data);

Set date of calendar

Ok, so what I'm trying to do is to set the set the date of a calendar instance, and then return the week_of_year. I am doing so by using the Calendar.set() functio
public String weekInYearForm = "ww";
SimpleDateFormat formWIM = new SimpleDateFormat(weekInYearForm, Locale.US);
Calendar lc = Calendar.getInstance();
lc.set(Calendar.YEAR, lYear);
lc.set(Calendar.MONTH, lMonth);
lc.set(Calendar.DAY_OF_MONTH, lDay);
wiy = formWIM.format(lc.get(Calendar.WEEK_OF_YEAR));
To get the lYear, lMonth, and lDay values, I am passing a string in the format 04/26/2013 to through the following steps:
String[] arrDate = dateIn.split("/");
int lMonth = Integer.parseInt(arrDate[0]) - 1;
Log.d("SaveHandler", "Month is: " + lMonth);
int lDay = Integer.parseInt(arrDate[1]);
Log.d("SaveHandler", "Day is: " + lDay);
int lYear = Integer.parseInt(arrDate[2]);
Log.d("SaveHandler", "Year is: " + lYear);
The problem I am facing is that when I look at what is outputed to wiy, it is always 1. Upon some further debugging, I realized that the time is being left at epoch time, and not setting to the values I need.
I also tried using lc.set(lYear, lMonth, lDay), also to no avail. If anyone has any ideas, I would greatly appreciate them.
*EDIT: I did some debugging earlier and it is returning 1970 for the year and 0 for the month.
use
formWIM.format(lc.getTime());
instead of
formWIM.format(lc.get(Calendar.WEEK_OF_YEAR));
EDIT
You can parse your date (instead of dateIn.split( etc.)
SimpleDateFormat monthDayYear = new SimpleDateFormat("MM/dd/yyyy", Locale.US); //04/26/2013
Date date = monthDayYear.parse("04/26/2013");
and then format it
SimpleDateFormat formWIM = new SimpleDateFormat("ww", Locale.US);
formWIM.format(date);
This code is correct, the problem is in formWIM.format(...) or the battery of your motherboard clock is drained.

Stop date dropping zero's

How do I stop the ints dropping the zeros in the time? I've tried formatting it with
String.format("%02d", minutes);
but it doesn't work, I'm sure it's quite simple!
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
String curTime = hours + ":" + minutes;
String.format("%02d", minutes);
updatedat.setText("Updated at " + curTime);
Use a SimpleDateFormat object instead to format dates/times.
Date date = new Date(); // initializes to current time
DateFormat df = new SimpleDateFormat("h:mm");
updatedat.setText("Updated at " + df.format(date));
Read more about SimpleDateFormat and the formatting specifications here.
The reason it's not working is because String.format("%02d", minutes); is a function that returns a string
for your case if minutes was 8, String.format("%02d", minutes); would return 08
So, for this to work you'd have to have something like:
String curTime = hours + ":" + String.format("%02d", minutes);
I also agree that you shouldn't be formatting the time like this, use a date formatter.
One simple way of including 0's is to use a function like:
public String TwoCh(int i) { String s= "0" + Integer.toString(i); return s.substring(s.length() - 2); }
and then (notwithstanding the fact that getHours and getMinutes are both deprecated), you can do things like
String curTime = TwoCh(dt.getHours())+ ":" + TwoCh(dt.getMinutes());
I think what you want to do is to get the time in following format
22:01
In order to do that, use simpleDateFormat
you can read the full documentation, visit http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

String.format to get the output date in dd-MMM-YYYY HH:mm pattern (Java)

String.format(start.toString("dd-MMM-YYYY HH:mm"));
where start is a date input in LocalDateTime class from org.joda.time api
when i am using this code, its returning month like this "Dec" but i want the output as "DEC".
If you want a specific case, I would use .toUpperCase()
If this (String.format(start.toString("dd-MMM-YYYY HH:mm"));) retrieves the correct format of what you want, then you can simply use
String.format(start.toString("dd-MMM-YYYY HH:mm")).toUpperCase();
I always using substring, in my case like this :
String sDate = String.format(start.toString("dd-MMM-YYYY HH:mm"));
String oDate = sDate.substring(0, 2)+"-"+sDate.substring(3, 6).toUppercase()+"-"+sDate.substring(7, 11);
The only way will be to replace a substring by the uppercase version.
Date start = new Date();
SimpleDateFormat oFormat = new SimpleDateFormat("dd-MMM-YYYY HH:mm");
String sDate = oFormat.format(start);
System.out.println(sDate);
sDate = sDate.substring(0,3) + sDate.substring(3,6).toUpperCase() + sDate.substring(6,sDate.length());
System.out.println(sDate);

Categories

Resources