如何在GMT获得下周四的时间戳

时间:2014-07-24 11:49:17

标签: java android calendar simpledateformat

我正在制作倒数计时器,它将在GMT上运行。周四12:00格林尼治标准时间重置。然后它会再次倒计时。

我已经制作了倒数计时器。它接受2个输入。

1 - 当前TimeStamp 2 - 下周四的TimeStamp(格林威治标准时间00点00分)。假设它目前是格林威治标准时间24.7.2014 3:40 AM,那么下周四应该是同一天,时间戳应该是24.7.2014 12:00 GMT。 等等。

我遇到了第二部分的问题。我能够在我的本地时区(IST)获得它。我正在努力为GMT做到这一点。但事情变得搞砸了。我不知道我在哪里做错了。

愿意从这里清除我的概念。

代码是: -

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(GetNextThursday());

        System.out.println();

        System.out.println("Current TIME DETAILS According to currentTimeMills :");
        long endTime=System.currentTimeMillis();

        Timestamp stamp = new Timestamp(endTime);
        Date date = new Date(stamp.getTime());
        System.out.println(date);
        System.out.println(endTime);




    }

    public static long GetNextThursday() {
        SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
           Calendar c = Calendar.getInstance();

           int todayday=c.get(Calendar.DAY_OF_WEEK);

           switch(todayday)
           {

           case 1:
               //It's sunday
               c.add(Calendar.DATE, 4);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               break;
           case 2:
               //It's Monday
               c.add(Calendar.DATE, 3);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               break;
           case 3:
               //It's Tuesday
               c.add(Calendar.DATE, 2);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               break;
           case 4:
               //It's Wednsday
               c.add(Calendar.DATE, 1);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               break;
           case 5:
               //It's Thursday

               if(c.get(Calendar.AM_PM) == 0)
               {
                   c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               }
               else
               {
                   c.add(Calendar.DATE, 7);
                   c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);

               }
               break;

           case 6:
               c.add(Calendar.DATE, 6);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);
               break;

           case 7:
               c.add(Calendar.DATE, 5);
               c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 12, 00, 00);

           }

          System.out.println("Next Thursday Based On CURRECT TIME (Default)");
          System.out.println(c.getTime());
          System.out.println(c.getTimeInMillis());        

          System.out.println();

          TimeZone tz = TimeZone.getTimeZone("GMT");
          c.setTimeZone(tz);
          System.out.println(c.getTime());
          System.out.println(c.getTimeInMillis());


          System.out.println();

          return (c.getTimeInMillis());

    }

}

它给出的是正确的。它将在下周四12:00 AM给予TimeStamp。但它是在IST给它。我希望在GMT中也一样。

期待可能的解决方案。

PS:我试图在StackOverflow上找到解决方案,然后我在这里发布了它。也许解决方案在那里可用,但我无法找到1.

0 个答案:

没有答案