【问题标题】:how to convert specified time in particular timezone? [closed]如何转换特定时区的指定时间? [关闭]
【发布时间】:2013-08-07 11:36:34
【问题描述】:

我住在印度,所以我的时区是 IST,所以当我想安排与英国客户的会面时,我就把时间放在上午 10 点到 11 点这样的现场。现在如何在英国时区 GMT0000 中转换?

基本上我已经输入时间,然后插入后有一个下拉框,其中包含所有可用时区,然后当我选择英国时区时,如何使用 Java 转换此过程?

提前谢谢..告诉我解决我的问题.. 注意:我不想手动执行此操作?选择它的原因会被转换吗?这是我的任务..希望你能理解我的问题..

问候..

【问题讨论】:

  • java != javascript ==> 下定决心...
  • 哪个容易?我也有 javascript 的知识.. 所以告诉我哪个容易让你指导我? @assylias

标签: java timezone


【解决方案1】:

印度时间是GMT + 5.30。那么10.00 -5.30可以认为是GMT时间。

在java中你可以做这样的事情

   DateFormat df = new SimpleDateFormat("HH:mm");
   Date dateIST=df.parse("10:00"); // india time
   df.setTimeZone(TimeZone.getTimeZone("GMT"));
   System.out.println(df.format(dateIST));// This is UK time

【讨论】:

  • @Ruchira..对不起,我找不到你? :( 你想说什么?
  • 非常感谢@ruchira .. 我现在接受你的回答,你能支持我的问题吗,因为我不认为它被否决了? :(
【解决方案2】:

(JAVA 回答)在文本字段中,您只需要输入您的当地时间。从该文本字段中获取输入并将其传递到以下代码中。

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));//or whatever timezone u want.
    String gmtStrDate = simpleDateFormat.format(Calendar.getInstance().getTime());

或者您可能会发现此代码适合您的需要(您的问题不清楚):

    SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm");
    Date date = inputFormat.parse("time from textfield");//like 11:44
    inputFormat.setTimeZone((TimeZone.getTimeZone("IST")));

    SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");
    outputFormat.setTimeZone((TimeZone.getTimeZone("GMT")));
    String outputText = outputFormat.format(date);

现在 outputText 是您想要的英国格式时间。

注意:这里我假设您在文本字段中输入时间,例如 13:44。您可以在格式化程序中相应地更改它。

【讨论】:

  • @Raju..假设我在 IST 中的 8:00 等文本框中输入了值,我想将这个时间转换为另一个时区?怎么可能?基于下拉框的选择?
  • 在您的程序中,您始终可以在变量中包含文本框值。只需使用“inputFormat”格式化程序解析该值并应用您的本地时区。稍后您可以像我上面所做的那样将其转换为另一个时区,然后放入下拉列表中。你是要我写下拉代码吗?
猜你喜欢
  • 1970-01-01
  • 2010-11-07
  • 2016-06-29
  • 2018-11-28
  • 1970-01-01
  • 2012-03-08
  • 2014-08-24
  • 2013-10-23
  • 2023-03-10
相关资源
最近更新 更多