Time in specific format Android Java - java

I would like the time respresented as this "Thu Jan 01 08:40:38 GMT+01:00 1754"
so i did this :
Date datu = new Date();
datu.setYear(1754);
datu.setMonth(0);
datu.setDate(1);
DateFormat.format("%tc", datu);
// DATU = Thu Jan 01 08:40:38 GMT+01:00 3654
String startTime = datu.toGMTString();
now the problem is that i set the year on 1754 in the code. but as I print it out. there is 3654 ?
EDIT:
datu.setYear(1754-1900) can do the trick. but isn't there another way?

Please try like this :
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss 'GMT'Z yyyy");
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,1754);
c.set(Calendar.MONTH, 0); // JAN
c.set(Calendar.DATE, 1);
System.out.println(sdf.format(new Date(c.getTimeInMillis())));
Output
Tue Jan 01 05:23:55 GMT+0800 1754

What version of the Java API are you using?
I would suggest you use the java.text.SimpleDateFormat and java.util.Calendar instead.
You code would look something like this:
Calendar cal = Calendar.getInstance();
cal.set(1754, 0, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // <- customize your format
sdf.format(cal.getTime()); // <- get your string
The date.toGMTString() method is also deprecated so it's best to avoid it.

It seems that date.SetYear is deprecated. According to the documentation you should use Calendar.set(Calendar.YEAR, year) instead.

As you said, date.setYear(desired year-1900) fixes the problem. It is noteworthy that the method is deprecated it is better you use the Calendar class which doesnt require subtracting 1900 from the year. A code snippet that will achieve what you are doing using Calendar is giving below:
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);// note that the date object here will be datu according to
//your code. It could be any object of the Date class
int year = calendar.get(Calendar.YEAR);
Hope this helps.
Feel free to mark as answer if it solves your problem.
Good luck!

Related

Convert SAS date value to Java YYYY-MM-DD

I have SAS date objects stored as integer and they look like : 19725.
I am trying to write java code to convert the date to YYYY-MM-DD
I see in the documentation that the SAS date value is the number of days from 01 Jan 1960
For example:
02 Jan 1960 would return 1
04 June 2003 would return 15680
Could you give the java code for this conversion. ie. convert something like 19725 to the date format : YYYY-MM-DD
I try the logic below but 15680 gives 2003-01-06 and not 2003-06-04 as the output. Could anyone point the mistake.Thanks in advance.
int numdays = 15680;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.YEAR, 1960);
cal.add(Calendar.DAY_OF_MONTH, numdays);
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
System.out.println(strdate);
Month are 0-based, so you're setting your calendar to February, not January. This should fix the issue:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, Calendar.JANUARY);
// ...
In addition to RC's point about starting the month correctly with Calendar.JANUARY, your simpledateformat is wrong.
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
'DD' is day of year (so 340th day of the year is Dec 6). 'dd' is day of the month. See the doc for more detail. (Also note that 15680 is Dec 6 2002, not what you say in the question.)
You may actually want to use 'yy' also:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
as 'YYYY' is "Week Year", which in some cases may differ from yyyy (calendar year) near the end of the year. See the docs for more details.
I like to use JodaTime for date manipulation like this.
http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#plusDays-int-
int sasDate = 19725;
DateTime base = new DateTime(1960, 1, 1, 0, 0);
DateTime computed = base.plusDays(sasDate);

Changing the format of a complex date in Java

I am trying to return a validity date, or an expiration date for a file, if you will. Currently, I have a sample program that prints out all the needed information but nnot exactly in the format I get. My code prints out:
Thu May 15 04:57:36 PDT 2014
But I need it to print out like so:
Thu, 15 May 2013 04:57:36 PDT
As you can see, very similar just not quite in the right place. Here is my test code that gives me the first result:
import java.util.Calendar;
import java.util.Date;
public class TestCalendar{
public static void main(String[] args){
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(new Date());
currentDate.add(Calendar.MINUTE, 24);
Date newDate = currentDate.getTime();
System.out.println("Date: " + newDate);
}
}
This code adds 24 minutes to the current time and then prints it out, I just cant figure out how to reformat this date. SimpleDateFormat doesn't seem like it's going to work but I could be wrong. I just couldn't figure it out.
Thanks in advance for any help anyone is able to give me!!
Use SimpleDateFormat:
DateFormat formatter= new SimpleDateFormat("E, d MMM yyyy hh:mm:ss zzz");
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(new Date());
currentDate.add(Calendar.MINUTE, 24);
Date newDate = currentDate.getTime();
System.out.println(formatter.format(newDate));
System.out.println("Date: " + newDate);
invokes toString() on Date instance, which has fixed format, you can't modify that
what you need is to format() your Date instance to String using SimpleDateFormat

GMT in java, simple but not working

This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,

Mysql datetime to Calendar

My code:
Calendar c = Calendar.getInstance();
c.setTime(res.getDate("my_date")); //res is ResultSet
System.out.println(c.getTime());
System.out.println(res.getString("my_date"));
Output:
Thu Oct 11 00:00:00 IST 2012
2012-10-11 02:50:00.0
In calendar, it is not considering the time part. I used Calendar because I have to make some comparisons.
Why time part is omitted?
You should use something like :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
out.println(sdf.format(rs.getTimestamp("your_date_field").getTime()));
By the way, you have to create a SimpleDateFormat that fit your SQL date format.

java date problem in parsing

I am parsing a date, where I am passing 2011-02-10 00:00:00:0, and I am getting 2011-01-10 as the result. Is this wrong? Please help.
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
today = df.parse(datecollection);
The pattern for month is MM: yyyy-MM-dd. You should read the API doc.
You should be using MM not mm. MM is for month, mm is for minute
try using MM instead of mm should work
refer http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
M Month in year Month July; Jul; 07
m Minute in hour Number 30
an example
"yyyy-MM-dd HH:mm:ss:SSS" 2001-07-04 12:08:56:235
You need to replace mm with MM. Please take a look at this page.
try
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.parse("2011-02-10 00:00:00:0"));
}
catch (Exception e){}
Prints:
Thu Feb 10 00:00:00 CET 2011

Categories

Resources