You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Groovy中按指定格式格式化日期/时间?

Got it, let's break down how to generate that exact timestamp format (2018-01-26T10:47:44.686000-08:00) — it's an ISO 8601 extended format with microseconds and a fixed UTC offset (-08:00). I know official docs can be dense, so I'll keep this simple with practical examples for common languages:

1. Python

Python's built-in datetime module makes this super straightforward. You can either create the exact timestamp from your example, or generate the current time in the same format:

from datetime import datetime, timezone, timedelta

# Create the exact example timestamp
target_datetime = datetime(2018, 1, 26, 10, 47, 44, 686000, timezone(timedelta(hours=-8)))
print(target_datetime.isoformat())
# Output: 2018-01-26T10:47:44.686000-08:00

# Generate current time in the same format
current_datetime = datetime.now(timezone(timedelta(hours=-8)))
print(current_datetime.isoformat())
2. JavaScript (Browser/Node.js)

JS doesn't natively support microseconds in date objects, but we can manually construct the format to match your example. Here's how to create the exact timestamp or generate a current-time version:

// Create the exact example timestamp
const targetDate = new Date('2018-01-26T10:47:44.686-08:00');

// Helper function to format with microseconds and fixed offset
function formatISOWithMicro(date, offsetHours) {
  const pad = (num, length) => num.toString().padStart(length, '0');
  const year = targetDate.getFullYear();
  const month = pad(targetDate.getMonth() + 1, 2);
  const day = pad(targetDate.getDate(), 2);
  const hours = pad(targetDate.getHours(), 2);
  const minutes = pad(targetDate.getMinutes(), 2);
  const seconds = pad(targetDate.getSeconds(), 2);
  const ms = pad(targetDate.getMilliseconds(), 3);
  const micro = '000'; // For real microseconds, use `performance.now()` (browser) or `process.hrtime()` (Node)
  const offsetSign = offsetHours < 0 ? '-' : '+';
  const offsetStr = `${offsetSign}${pad(Math.abs(offsetHours), 2)}:00`;

  return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${ms}${micro}${offsetStr}`;
}

console.log(formatISOWithMicro(targetDate, -8));
// Output: 2018-01-26T10:47:44.686000-08:00
3. Java (Java 8+)

Java's modern java.time API handles this format natively without extra work:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class TimestampGenerator {
    public static void main(String[] args) {
        // Create the exact example timestamp
        OffsetDateTime targetDt = OffsetDateTime.of(
            2018, 1, 26, 10, 47, 44, 686000000, 
            java.time.ZoneOffset.ofHours(-8)
        );
        
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        System.out.println(formatter.format(targetDt));
        // Output: 2018-01-26T10:47:44.686000-08:00

        // Generate current time in the same format
        OffsetDateTime nowDt = OffsetDateTime.now(java.time.ZoneOffset.ofHours(-8));
        System.out.println(formatter.format(nowDt));
    }
}

Quick breakdown of the format parts to help you tweak it if needed:

  • YYYY-MM-DD: Standard date format
  • T: Separator between date and time
  • HH:MM:SS.sssfff: Time with seconds, milliseconds, and microseconds
  • -08:00: Fixed UTC offset (Pacific Standard Time in this case)

内容的提问来源于stack exchange,提问作者Benihana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:53