如何在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:
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())
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
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 formatT: Separator between date and timeHH:MM:SS.sssfff: Time with seconds, milliseconds, and microseconds-08:00: Fixed UTC offset (Pacific Standard Time in this case)
内容的提问来源于stack exchange,提问作者Benihana

